如何在UITableView行之间创建工具栏

ahe*_*ang 25 iphone user-interface objective-c uitableview

我对tweetbot如何执行以下操作感兴趣:

在此输入图像描述

我想用我的应用程序创建相同的东西,如果你点击一行,它会弹出一个额外的UIToolBar并按下任何其他行将使用动画关闭此视图.

我认为逻辑很简单,你只需要在按下时将一个子视图添加到UITableViewCell并将其余内容向上移动,但是当我按下另一行时你如何实际解除它?

rjg*_*nzo 29

执行此操作的最佳方法是在轻敲的单元格下方添加虚拟单元格.

首先,您需要跟踪被挖掘的细胞并采取相应措施.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //if user tapped the same row twice let's start getting rid of the control cell
    if([indexPath isEqual:self.tappedIndexPath]){
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }

    //update the indexpath if needed... I explain this below 
    indexPath = [self modelIndexPathforIndexPath:indexPath];

    //pointer to delete the control cell
    NSIndexPath *indexPathToDelete = self.controlRowIndexPath;

    //if in fact I tapped the same row twice lets clear our tapping trackers 
    if([indexPath isEqual:self.tappedIndexPath]){
        self.tappedIndexPath = nil;
        self.controlRowIndexPath = nil;
    }
    //otherwise let's update them appropriately 
    else{
        self.tappedIndexPath = indexPath; //the row the user just tapped. 
        //Now I set the location of where I need to add the dummy cell 
        self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1   inSection:indexPath.section];
    }

    //all logic is done, lets start updating the table
    [tableView beginUpdates];

    //lets delete the control cell, either the user tapped the same row twice or tapped another row
    if(indexPathToDelete){
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete] 
                          withRowAnimation:UITableViewRowAnimationNone];
    }
    //lets add the new control cell in the right place 
    if(self.controlRowIndexPath){
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath] 
                          withRowAnimation:UITableViewRowAnimationNone];
    }

    //and we are done... 
    [tableView endUpdates];  
} 
Run Code Online (Sandbox Code Playgroud)

每当你有虚拟细胞存在时,你必须确保发送正确的计数.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.controlRowIndexPath){
        return modelArray.count + 1;
    }
    return self.modelArray.count;
}
Run Code Online (Sandbox Code Playgroud)

此外,返回ControlCell的适当高度.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath   *)indexPath 
{
    if([indexPath isEqual:self.controlRowIndexPath]){
        return 45; //height for control cell
    }
    return 70; //height for every other cell 
}
Run Code Online (Sandbox Code Playgroud)

最后,记住控制单元是假的.不是模型的一部分,因此您必须考虑到这一点.如果用户点击最后一个点击行上方的行是正常的,但是当新的点击行位于该控制单元格下方时,您必须确保访问模型中的右侧行.换句话说,在视图中间考虑该假单元格.

- (NSIndexPath *)modelIndexPathforIndexPath:(NSIndexPath *)indexPath
{
    int whereIsTheControlRow = self.controlRowIndexPath.row;
    if(self.controlRowIndexPath != nil && indexPath.row > whereIsTheControlRow)
        return [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0]; 
    return indexPath;
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

  • 基本上:`if(self.controlRowIndexPath && [indexPath isEqual:self.controlRowIndexPath])`>返回控制菜单的单元格.没有源代码,抱歉.我密切关注WWDC会议,并通过试验/错误找出其余部分. (2认同)

Mat*_*uch 18

tableView:didSelectRowAtIndexPath:,您从最后选择的单元格中删除工具视图.如果没有这样的视图,则创建一个新视图.然后将此视图添加到新选择的单元格中.保存所选行的indexPath.

tableView:heightForRowAtIndexPath:,您检查indexPath是否与保存的indexPath相同.如果它们相等,则返回高度,即两个视图的高度.如果它不相等,只需返回"真实单元格"的高度.

将所有调用放在didSelectRowAtIndexPath两者之间[tableView beginUpdates],[tableView endUpdates]以获得高度变化的动画.