【问题标题】:iPhone - How add a final line in a UITableView to add items, preventing rearranging of this lineiPhone - 如何在 UITableView 中添加最后一行以添加项目,防止重新排列此行
【发布时间】:2011-03-27 16:02:23
【问题描述】:

我有一个 UITableView 处于编辑模式。

 self.tableView.editing = YES;

在这个表格视图中,我在自定义单元格中显示了一些行,我想在最后添加一个允许用户添加项目(使用另一个视图)。

所以我写了几行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return "number of lines" + 1;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.row+1 != [tableView numberOfRowsInSection:0]) {   
        return UITableViewCellEditingStyleDelete;
    }
    else {
        return UITableViewCellEditingStyleInsert;
    } 
}

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) {
        cell.textLabel.text = @"Add a line...";
    }
    else {
        do the stuff in the custom cell
    }
}

这样做,UITableView 允许重新排列任何行。我可以在“添加行”之后移动第一行,并将“添加行”移动到第一个位置。

如何删除“添加行”单元格上的排列按钮并防止其他行进入此行?

或者有更好的编码方式吗?

【问题讨论】:

    标签: iphone uitableview editing


    【解决方案1】:

    实施

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return ([indexPath row] != INDEX_OF_SPECIAL_ROW)
    }
    

    - (NSIndexPath *)tableView:(UITableView *)tableView 
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
           toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
    {
        if ([proposedDestinationIndexPath row] < NUMBER_OF_ROWS)
        {
            return proposedDestinationIndexPath;
        }
    
        NSIndexPath *otherPath = [NSIndexPath indexPathForRow:NUMBER_OF_ROWS-1 inSection:0];
    
        return otherPath;
    }
    

    【讨论】:

      【解决方案2】:

      更简单的方法是设置tableFooterView。您可以在其中放置任何 UIView,因此您不仅可以添加 UITableViewCell,还可以添加完全自定义的子类。这样做可以避免所有不必要的检查。

      【讨论】:

        【解决方案3】:
        return UITableViewCellEditingStyleNone
        

        对于- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath中的那一行

        然后查看– tableView:moveRowAtIndexPath:toIndexPath:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-17
          • 1970-01-01
          • 1970-01-01
          • 2021-06-16
          • 1970-01-01
          • 1970-01-01
          • 2015-12-05
          • 2015-10-19
          相关资源
          最近更新 更多