UITableViewCell滑动但删除按钮不会出现

时间:2011-02-04 18:49:40

标签: objective-c uitableview ios-simulator

这很奇怪。我正在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];

    }   
}

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

3 个答案:

答案 0 :(得分:10)

如果您只想在桌面上启用滑动即可删除,则可以采用更简单的方法。在数据源中实现tableView:commitEditingStyle:forRowAtIndexPath:,并且在刷新单元格时,表格视图将自动显示删除按钮。

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

答案 1 :(得分:0)

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

 swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];

答案 2 :(得分:-1)

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

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