【问题标题】:UITableViewCell swiped but delete button doesn't appearUITableViewCell 刷卡但删除按钮没有出现
【发布时间】:2011-06-21 13:40:03
【问题描述】:

这很奇怪。我在 iPad 模拟器中刷了一个 UITableViewCell。即使下面的事件触发并且 swipedCell 不是 nil,删除按钮也不会出现。实际上,它会出现——但只是有时。我从来没有得到错误的访问权限或 sigbart。

代码如下:

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer 
{   
    if (userListSwipeRightRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [userListSwipeRightRecognizer locationInView:self.outletView];
        NSIndexPath *swipedIndexPath = [self.outletView indexPathForRowAtPoint:swipeLocation];
        UITableViewCell* swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];
        [swipedCell setEditing:YES];

    }   
}

这只是模拟器问题还是我做错了什么?

【问题讨论】:

  • 你有什么理由自己处理滑动手势,而不是让 UITableView 自动完成?
  • 我还将在滑动单元格时在辅助单元格中显示一个“编辑”按钮。我正在做的可能不是最好的方法。这是我的第一个应用程序。

标签: objective-c uitableview ios-simulator


【解决方案1】:

如果您使用自定义单元格并覆盖setEditing,则必须调用超级方法,否则您的删除控件将不会被绘制。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
}

【讨论】:

    【解决方案2】:

    如果您只是想在您的桌子上启用滑动删除,有一种更简单的方法可以做到这一点。在你的数据源中实现tableView:commitEditingStyle:forRowAtIndexPath:,当滑动单元格时,表格视图将自动显示删除按钮。

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    

    【讨论】:

    • 我认为这实际上会删除行。我只是想让删除按钮出现....
    • 如果定义了该方法,表格视图将自动显示按钮。单击按钮时将调用该方法。如果您实际上不想删除按钮,请不要在方法内调用 deleteForRowsAtIndexPaths:。
    • 很好的提示,我也首先尝试让按钮在滑动时可见,认为我还不需要能够“提交”操作......而只是实现一个空方法使刷卡工作
    【解决方案3】:

    如果您在标题中定义了 UITableView,请尝试:

     swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];
    

    【讨论】:

    • 我已经在 OP 代码中这样做了。你的意思是别的还是我误解了你?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多