iOS - テーブルビューの最後の項目の IndexPath を取得するにはどうすればよいですか? 質問する

iOS - テーブルビューの最後の項目の IndexPath を取得するにはどうすればよいですか? 質問する

テーブルビューの最後まで自動的にスクロールしたいと思います。

[tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

以下を使用してテーブルビューにいくつのアイテムがあるかがわかっているとします。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

このテーブルビューの最後の項目への IndexPath * を取得するにはどうすればよいですか? これは、scrollToRowAtIndexPath:atScrollPosition:animated への引数として提供するために必要です。

ありがとう!

ベストアンサー1

最後のセクションの最後の行への参照を取得するには…

// First figure out how many sections there are
NSInteger lastSectionIndex = [tableView numberOfSections] - 1;

// Then grab the number of rows in the last section
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;

// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];

おすすめ記事