Objective c iOS-从UITableView中删除行不会';不动
我有一个非常令人沮丧的问题。我有一个带有UITableView的应用程序。 当我从表视图中删除单元格时,它将从数据模型中删除,然后我调用以下命令:Objective c iOS-从UITableView中删除行不会';不动,objective-c,ipad,uitableview,ios6.1,Objective C,Ipad,Uitableview,Ios6.1,我有一个非常令人沮丧的问题。我有一个带有UITableView的应用程序。 当我从表视图中删除单元格时,它将从数据模型中删除,然后我调用以下命令: -(void)removeItem:(NSIndexPath *)indexPath { [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:
-(void)removeItem:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
我的问题是,我像上面一样尝试过,而且我没有使用animateWithDuration。我甚至尝试过使用CATTransaction,但不管我怎么做,动画都不会出现
我的模拟器中有慢动画,当我从列表中删除一个项目时,它会正确删除,但没有动画。
在重新加载表视图数据之前,它会消失并留下一段空白
我已经搜索了这么多和谷歌,我似乎找不到答案。
有什么想法吗
这可能与我在调用上面的函数之前从数据模型中删除对象有关吗
编辑:删除动画块,因为它不正确根据您根本不需要使用动画持续时间:动画:
就这样试试吧
-(void)removeItem:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
首先,您不需要自己设置更改的动画 其次,我认为您需要在开始和结束更新之间对数据源进行更改 您的方法应该如下所示:
-(void)removeItemAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView beginUpdates];
// I am assuming that you're just using a plain NSMutableArray to drive the data on the table.
// Delete the object from the datasource.
[self.dataArray removeObjectAtIndex:indexPath.row];
// Tell the table what has changed.
[self.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
好吧,我不知道这是否只是不礼貌,但我觉得我要在这里回答我自己的问题 首先,感谢所有其他的答案,你们当然都是正确的,但没有一个答案解决了我的问题 事实证明,代码中还有另一个区域执行不同的检查,然后调用其中一个tableView委托方法,这似乎会取消动画 因此,答案如下: 当您正在删除行但动画不起作用时,请确保在动画开始之前未调用DidSelectRowatineXpath:indexPath。这将取消动画
如果没有这个问题,下面是一些用于扩展的典型代码,示例中有两行: 请注意,facebookRowsExpanded是一个类变量,必须具有:
if ( [theCommand isEqualToString:@"fbexpander"] )
{
NSLog(@"expander button......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray *deleteIndexPaths;
NSArray *insertIndexPaths;
facebookRowsExpanded = !facebookRowsExpanded;
// you must do that BEFORE, not AFTER the animation:
if ( !facebookRowsExpanded ) // ie, it was just true, is now false
{
deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
deleteRowsAtIndexPaths:deleteIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
else
{
insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
// DO NOT do this at the end: [_superTableView reloadData];
return;
}
注意:节中的行数代码必须使用facebook行扩展
(类似于“如果facebookRowsExpanded返回7,否则返回5”)
注意:cellForRowAtIndexPath的代码必须使用FaceBookRowExpanded
(它必须返回正确的行,具体取决于是否展开。)
IndExpath是要在表中插入或删除的NSIndExpath数组
NSarray *indexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];
尝试将其从
animateWithDuration:animations:
块中删除。表视图自己处理动画,您不需要做任何其他事情。请向大家展示您的提交方式:
方法,但正如我在问题中所说的,我也尝试了不使用动画宽度:动画,但这并不能解决问题@瓦伦丁:很快就会出现。我对这部分代码和iOS基本上都是新手。我看到我们没有实现CommittedItingStyle:方法。从数据模型中实际删除是在RESTKit调用中的删除块中进行的,如果这样做有区别的话。我建议您不要编辑动画块,因为这完全是错误的。否则,您共享的代码看起来是正确的。但你并没有分享太多。我想知道你还做了什么会打断动画。您说过“在重新加载表视图数据之前”。我不知道你这是什么意思。您没有调用reloadData
是因为这可能会破坏动画吗?谢谢大家,但正如我在问题中所说的,我也尝试过不使用动画宽度:动画:但这并不能解决问题。我会看看这是否可行。从数据模型中删除是复杂的,并且发生在完全不同的位置,在一个耦合到RESTKit调用的块中。我要试一试,看看是否可能。我只是不知道这是否是问题所在。由于从tableview中删除可以100%工作,它只是没有动画。今天我遇到了同样的问题,在我的例子中,调用reloadData方法可以取消动画。通过删除reloadData方法恢复了动画,我看到您也在代码末尾写了一条注释。
NSarray *indexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];