Ios UITableView正确动画
问题是,我想要一个分组表视图,当我点击一个单元格时,我想要另一个单元格出现在它下面,依此类推。工作流程一切正常,但我在动画方面也有同样的问题。它工作非常缓慢,我可以看到所有的变化Ios UITableView正确动画,ios,uitableview,animation,Ios,Uitableview,Animation,问题是,我想要一个分组表视图,当我点击一个单元格时,我想要另一个单元格出现在它下面,依此类推。工作流程一切正常,但我在动画方面也有同样的问题。它工作非常缓慢,我可以看到所有的变化 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"selected section:%d selected row:%d",indexPath.section,i
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"selected section:%d selected row:%d",indexPath.section,indexPath.row);
NSIndexPath* pathToDelete;
[_tableView beginUpdates];
if (_selectedPath != nil && [_selectedPath isEqual:indexPath]){
//If the same row was selected
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row+1 inSection:_selectedPath.section];
_selectedPath = nil;
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
} else {
//If not
if (_selectedPath != indexPath){
//delete the row that we selected last time
if (_selectedPath != nil){
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row inSection:_selectedPath.section];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
//add new row in the section we selected
_selectedPath = indexPath;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
}
[_tableView beginUpdates];
[_tableView deselectRowAtIndexPath:indexPath animated:NO];
[_tableView endUpdates];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:pathToDelete.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:_selectedPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView endUpdates];
}
我认为问题在于begin&endUpdates方法,但我不知道具体在哪里。有线索吗?
谢谢您可以通过使用单个
beginUpdates
-endUpdates
来简化此操作。您只需在其中进行删除和插入。别忘了更新表的数据源,否则应用程序会崩溃
希望这有帮助 你有什么问题?