我的 View 上有一个UITableView
,我想应用某个部分的滑动删除模式行。我实现的如下:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">> canEditRowAtIndexPath");
if (indexPath.section == CanDeletedSection) {
return YES;
}else{
return NO;
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">> editingStyleForRowAtIndexPath");
if (indexPath.section == CanDeletedSection) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">> commitEditingStyle");
if (editingStyle == UITableViewCellEditingStyleDelete) {
// dosomething
}
}
但是当我滑动表格行时,有时会出现 Delete
按钮,有时不会。
顺便说一下,我的单元格是自定义的并继承自 UITableViewCell
。
我已将 NSLog
添加到上述方法中。当 Delete
按钮没有出现时,我得到的日志是这样的:
>> editingStyleForRowAtIndexPath
>> canEditRowAtIndexPath
当出现Delete
按钮时,日志如下:
>> editingStyleForRowAtIndexPath
>> canEditRowAtIndexPath
>> editingStyleForRowAtIndexPath
>> canEditRowAtIndexPath
>> canEditRowAtIndexPath
>> editingStyleForRowAtIndexPath
我制作了一个使用自定义单元格的演示,效果很好。所以问题是由包含 TableView 的 View Controller 引起的。 View Controller 继承自另一个 View Controller ,在那个 View Controller 中,有一个用于隐藏键盘的点击手势。但是当我从 View Controller 中删除它们时,结果是一样的。
最佳答案
请检查view或superview是否有其他手势。如果是这样,请确保在设置手势委托(delegate)后实现 UIGestureRecognizerDelegate
的以下方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
关于iphone - 为什么 UITableView 的滑动删除有时工作正常有时不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13929703/