如何使最后一行无法删除

约瑟

我有一个显示您的朋友的表格视图,并且正在使该表格可编辑。您可以通过点击最后一行“添加朋友”来添加朋友。当您要删除朋友时,请点击导航栏上的“编辑”,然后可以删除所需的任何行。问题是:您还可以删除最后一行“ ADD FRIEND”。如何使最后一行不可删除?我不想在最后一个单元格(红色圆圈)上显示“可删除”动画。

代码很基本,请参见下文。

.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendID" forIndexPath:indexPath];

    // Configure the cell...

    NSInteger totalRow = [tableView numberOfRowsInSection:indexPath.section]; //first get total rows in that section by current indexPath.

    if(indexPath.row == totalRow -1){
        //this is the last row in section.
        AddFriendIndexRow = totalRow-1;
        cell.friendName.text = @"+  ADD FRIEND";
    } else {
        cell.friendName.text = friendsList[indexPath.row];
    }
    return cell;
}




// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    [friendsList removeObjectAtIndex:indexPath.row];
    NSLog(@"friendslist: %@", friendsList);


    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];


    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
杰夫

您将要使用canEditRowAtIndexPath数据源方法并执行以下操作:

- (BOOL)tableView:(UITableView * _Nonnull)tableView canEditRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath {
    if (indexPath == //Last index path)
        return NO
    else return YES
}

您要为tableView中的最后一个indexPath返回NO,而为其他所有返回YES。这将使除最后一行之外的所有行都可编辑。可以在Apple网站上找到更多文档:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/#//apple_ref/occ/intfm/UITableViewDataSource/tableView:canEditRowAtIndexPath:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章