【问题标题】:UITableView commitEditingStyle without deleting cellUITableView commitEditingStyle 不删除单元格
【发布时间】:2012-07-26 09:02:30
【问题描述】:

我对删除 UITableView 中的行的概念有一些小问题。用户删除一行后我不想触发:

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

相反,我想更改已编辑的单元格文本和背景而不隐藏它。我的方法效果很好:

if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSMutableArray *previous_points = [self.trip_arrayOfAllPoints mutableCopy];
    [previous_points removeObjectAtIndex:indexPath.row];
    self.trip_arrayOfAllPoints = nil;

    self.trip_arrayOfAllPoints = [[NSMutableArray alloc] initWithCapacity:[previous_points count]];
    self.trip_arrayOfAllPoints = [previous_points mutableCopy];

    self.trip_arrayOfAllPointsEDITED = [[NSMutableArray alloc] initWithCapacity:[previous_points count]];
    self.trip_arrayOfAllPointsEDITED = [previous_points mutableCopy];
    //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

    self.trip_isArrayOfAllPointsEDITED = YES;
}

但在删除一行后,它不会在当前单元格上提交消失“DELETE”标签的动画。有没有办法绕过“Apple方式”删除行?

编辑 - 我已经解决了

它非常简单明了。只需将 deleteRowsAtIndexPaths 替换为:

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

在这个“删除”动画消失后,单元格仍在原处。

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    你的意思是“删除”按钮会保留,即使你按下它?在我的 iOS 5.1 模拟器上之后 用空按“删除”:

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

    “删除”按钮消失。

    【讨论】:

    • 将此方法留空,看看当您按下删除时会发生什么。只需 /* 开头,*/ 结尾。
    • 谢谢你,在你发帖后,我决定将方法留空并使用我的单元格进行操作。这解决了我的问题(太简单了):)。
    【解决方案2】:

    我正在使用此代码,它运行良好

    //self.selectedMessageIndex is NSIndexPath object declared in header file
    //self.attachments is a NSMutableArray Object
    
    [self.attachments removeObjectAtIndex:self.selectedMessageIndex.row];
    
    [tblvAttachmentsList deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.selectedMessageIndex]
                                      withRowAnimation:UITableViewRowAnimationLeft];
    [tblvAttachmentsList reloadData];
    

    【讨论】:

    • 我真的不想提交删除动画。我只想排在原地,但颜色/背景不同等。这就是问题所在。
    • 我们真的需要在这里使用“reloadData”吗?我认为没有必要。
    【解决方案3】:
    self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:.Automatic)
    

    这将删除带有漂亮动画的删除按钮。调用,当您知道您不会删除该行时:

    func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath:NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
    
            //An action happens here. If it was successful, page is refreshed
            //If the action fails, cell stays. I am showing an error and:
            self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:.Automatic)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2017-05-24
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多