在ios中滚动的可扩展列表视图

时间:2014-10-22 08:27:41

标签: ios uitableview

我使用表格视图作为可扩展列表视图。我使用section作为父级,单元格作为child.I我正在获得列表视图完美但问题是我希望展开的剖面视图进入视图如果该部分单击屏幕末尾。目前它扩展并保持在那里,因此必须手动滚动它。 感谢。

1 个答案:

答案 0 :(得分:3)

最简单的方法是拨打
-[UITableView scrollToRowAtIndexPath: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 scrollToRowAtIndexPath:atScrollPosition:animated:]
直接在-[UITableView reloadData]

之后 像这样:

[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];
相关问题