LoginSignup
26
23

More than 5 years have passed since last update.

UITableView で任意の位置にスクロールさせる

Last updated at Posted at 2014-05-30

scrollToRowAtIndexPath を使う

スクロールしたい Cell の section, index を渡してあげる。

NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

データを更新したあとにスクロールさせたい

データを更新する場合、UITableView のデータを更新が終わったことを検知するような delegate はなさそう。なので、TableView の reload が終わったあとに実行されるよう dispatch_async を使ったところうまく更新できた。

int row = 5; // exmample
[self.tableview reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
26
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
23