UITableViewController:如果满足条件,则滑动以删除

时间:2013-04-04 13:58:57

标签: iphone ios ios6 uitableview swipe

对于UIViewController重用目的,我希望仅在满足条件的情况下允许“滑动删除”手势。有没有办法实现这个目标?

如果我添加以下UITableViewController委托方法:

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

启用了“滑动删除”,但我无法区分我要禁用此手势的情况

3 个答案:

答案 0 :(得分:5)

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
        editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL someCondition = // figure out whether you want swipe to be available
    return (someCondition) ?
        UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}

来自本书这一部分的结尾:

http://www.apeth.com/iOSBook/ch21.html#_deleting_table_items

答案 1 :(得分:0)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (UITableViewCellEditingStyleDelete==YES)
    {
          // here goes your code
    }
}

答案 2 :(得分:0)

将您的逻辑放在委托方法

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

例如

仅允许编辑奇数行:

-(UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
        editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 1)
    {
        return UITableViewCellEditingStyleDelete;
    }
    else return UITableViewEditingStyleNone;
}

百分号?!

这是我喜欢教学的原因,因为它在很多情况下都非常有用 - 在代码块indexPath.row 2 == 1中,检查indexPath的行是否为奇数。以下是它的工作原理:百分号称为模数。它的作用就像你在一张纸上一样,然后进行计算的其余部分 - 一旦你理解了这一点,你就会看到它将是多么强大。例如,您可以检查数字是否可以被另一个数字整除。

在我们的例子中,我们看看当我们将行除以2时剩余部分是什么。如果它为0,我们知道它可以被2整除,因此它是偶数。但是,如果它返回1,则该行是奇数。这是一个了不起的工具。

相关问题