ios中的可扩展列表视图滚动
我使用表视图作为可扩展列表视图。我使用节作为父级,单元格作为子级。我获得的列表视图非常完美,但问题是,如果单击屏幕末尾的节,我希望扩展的节视图进入视图。目前,它扩展并停留在那里,因此必须手动滚动它。ios中的可扩展列表视图滚动,ios,uitableview,Ios,Uitableview,我使用表视图作为可扩展列表视图。我使用节作为父级,单元格作为子级。我获得的列表视图非常完美,但问题是,如果单击屏幕末尾的节,我希望扩展的节视图进入视图。目前,它扩展并停留在那里,因此必须手动滚动它。 谢谢。最简单的方法就是打电话 -[UITableView ScrollToRowatineXpath:atScrollPosition:animated: 但是要小心。如果在为节插入行之后立即调用该函数,动画可能看起来很糟糕(单元格从奇怪的位置飞入等)。最简单的解决方案是在行插入动画完成后执行。不幸
谢谢。最简单的方法就是打电话
-[UITableView ScrollToRowatineXpath:atScrollPosition:animated:
但是要小心。如果在为节插入行之后立即调用该函数,动画可能看起来很糟糕(单元格从奇怪的位置飞入等)。最简单的解决方案是在行插入动画完成后执行。不幸的是,没有回调,最简单的解决方法是使用CATransaction回调,如下所示:
// CATransaction is used to be able to have a callback after rows insertion is finished.
// This call opens CATransaction context
[CATransaction begin];
// This call begins tableView updates (not really needed if you only make one insertion call, or one deletion call, but in this example we do both)
[tableView beginUpdates];
// Insert and delete appropriate rows
[tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
// completionBlock will be called after rows insertion/deletion animation is done
[CATransaction setCompletionBlock: ^{
// This call will scroll tableView to the top of the 'section' ('section' should have value of the folded/unfolded section's index)
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:section] // you can pass NSNotFound to scroll to the top of the section even if that section has 0 rows
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}];
// End table view updates
[tableView endUpdates];
// Close CATransaction context
[CATransaction commit];
如果在没有动画的情况下进行折叠/展开,例如使用纯-[UITableView reloadData]
,则可以安全地调用
-[UITableView ScrollToRowatineXpath:atScrollPosition:animated:
直接在
-[UITableView重载数据]
像这样:
[tableView reloadData];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:section] // 'section' is the index of the section you want to be scrolled to the top of the screen
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
如果在点击小节标题后调用
insertRowsAtIndexPaths:
,然后在适当的位置添加我的示例中的CATransaction
行,那么应该这样做。如果您不使用insertRowsAtIndexPaths:
而只是reloadData
,那么您可以在reloadData
之后立即调用scrollToRowAtIndexPath:
,我真的得到了逻辑:(如果你不介意的话,你能解释一下代码在做什么吗?@VaisakhVinod我已经编辑了答案并添加了一些注释。如果有什么不清楚的地方,问:)