ios - UITableView - 控制或防止删除部分中的特定行

标签 ios objective-c uitableview

我试图阻止删除我的 UITableView 控件的每个部分中的每一行的最后一行。我编写了目前可以正常工作的代码。

在 UITableView 的编辑模式下,是否有办法防止删除按钮出现在特定部分的特定行?

代码如下:

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    // If table view is asking to commit a delete command...
    if ( editingStyle == UITableViewCellEditingStyleDelete) {
        // Prevent deleting the last row
        int length = [[[MyItemStore sharedStore] getItemsForGivenSection:section] count];

        // PREVENT LAST ROW FROM DELETING
        if ( row == length) {
            return;
        }

        NSArray *items = [[MyItemStore sharedStore] getItemsForGivenSection:section];
        MyItem *item = items[row];
        [[MyItemStore sharedStore] removeItemFromSection:item fromSection:section];

         //Also remove that row from Table view with animation
        [tableView deleteRowsAtIndexPaths: @[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

最佳答案

你可以使用这样的东西:

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    //Return FALSE for the last row
    if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1)
        return FALSE;
    }

    //Return TRUE for all other rows
    return TRUE;
}

关于ios - UITableView - 控制或防止删除部分中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976897/

相关文章:

ios - canOpenURL 成功但 openURL 失败,对于 tel 作为 urlschema

iOS 8 Metal 模板坏了

ios - 变量改变值时调用方法

iphone - 分组 UITableView 中节标题和第一个单元格之间的透明像素行

ios - 无论是通过手刹还是通过ffmpeg,我都无法获得1334x750的分辨率

ios - 在 Xcode 中将 View Controller 连接到侧边栏

objective-c - Cocoa App 启动闪屏

ios - dealloc 问题后的 dispatch_after

ios - NS无效参数异常 : "Unrecognized selector sent to instance"

ios - 我需要 UIView 上的 UITableView 帮助( Storyboard项目)