[英]iOS - How can I get the IndexPath of the last item in a tableview?
我想自動滾動到表格視圖的末尾。
[tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
要獲得對最后一節中最后一行的引用...
// 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];
您可以像這樣獲取上一節中最后一行的indexPath 。
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)];
這里, numberOfSections是從numberOfSectionsInTableView:
方法返回的值。 並且, numberOfRowsInLastSection是從表視圖中最后一節的numberOfRowsInSection:
方法返回的值。
這可以放在子類或類別中以簡化:
-(NSIndexPath*)indexPathForLastRow
{
return [NSIndexPath indexPathForRow:[self numberOfRowsInSection:self.numberOfSections - 1] - 1 inSection:self.numberOfSections - 1];
}
在快速3
let lastRowIndex = tableView.numberOfRows(inSection: tableView.numberOfSections-1)
if (indexPath.row == lastRowIndex - 1) {
print("last row selected")
}
可能有人會對UICollectionView
需要相同的UICollectionView
,所以我更新了Ryan Grimm的答案:
NSInteger lastSectionIndex = [self.collectionView numberOfSections] - 1;
NSInteger lastItemIndex = [self.collectionView numberOfItemsInSection:lastSectionIndex] - 1;
NSIndexPath *pathToLastItem = [NSIndexPath indexPathForItem:lastItemIndex inSection:lastSectionIndex];
嘗試這個:
在cellForRowAtIndexPath
if(indexPath.row == myArray.count -1)
{
myIndexPath = indexpath;
}
myIndexPath應該是NSIndexPath
對象
這是Swift 4.x中的擴展,考慮到可能沒有任何行,因此它避免了超出范圍的崩潰。
import UIKit
extension UITableView {
func lastIndexpath() -> IndexPath {
let section = max(numberOfSections - 1, 0)
let row = max(numberOfRows(inSection: section) - 1, 0)
return IndexPath(row: row, section: section)
}
}
然后從viewcontroller中調用它:
let lastIndexPath = tableView.lastIndexPath()
這里的大多數答案,包括已接受的答案,都沒有考慮具有零部分的表視圖或具有零行的最終部分的有效情況。
表示這些情況的最佳方法是使用NSNotFound
的索引,UIKit在諸如scrollToRowAtIndexPath:atScrollPosition:animated:
等方法中接受該索引。
NSNotFound是一個有效的行索引,用於滾動到零行的節。
我使用以下方法:
- (NSIndexPath *)bottomIndexPathOfTableView:(UITableView *)tableView
{
NSInteger finalSection = NSNotFound;
NSInteger finalRow = NSNotFound;
NSInteger numberOfSections = [tableView numberOfSections];
if (numberOfSections)
{
finalSection = numberOfSections - 1;
NSInteger numberOfRows = [tableView numberOfRowsInSection:finalSection];
if (numberOfRows)
{
finalRow = numberOfRows - 1;
}
}
return numberOfSections ? [NSIndexPath indexPathForRow:finalRow inSection:finalSection] : nil;
}
似乎所有解決方案都沒有考慮到最后一節可能根本沒有行 。 所以這是一個函數,它返回最后一個非空部分的最后一行:
斯威夫特3:
extension UITableViewDataSource {
func lastIndexPath(_ tableView: UITableView) -> IndexPath? {
guard let sections = self.numberOfSections?(in: tableView) else { return nil }
for section in stride(from: sections-1, through: 0, by: -1) {
let rows = self.tableView(tableView, numberOfRowsInSection: section)
if rows > 0 {
return IndexPath(row: rows - 1, section: section)
}
}
return nil
}
}
例:
class ViewController: UIViewController {
//...
func scrollToLastRow() {
if let indexPath = lastIndexPath(tableView) {
tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
}
extension ViewController: UITableViewDataSource {
//required data source methods
}
Swift 3回答,超出界限檢查。 實現為tableview擴展
extension UITableView {
var lastIndexPath: IndexPath? {
let lastSectionIndex = numberOfSections - 1
guard lastSectionIndex >= 0 else { return nil }
let lastIndexInLastSection = numberOfRows(inSection: lastSectionIndex) - 1
guard lastIndexInLastSection >= 0 else { return nil }
return IndexPath(row: lastIndexInLastSection, section: lastSectionIndex)
}
}
試試這段代碼:
// get number of section
let indexOfLastSection = self.yourTableView.numberOfSections - 1
// Then get the number of rows in the last section
if indexOfLastSection >= 0{
let indexOfLastRow = self.yourTableView.numberOfRows(inSection: indexOfLastSection) - 1
if indexOfLastRow >= 0{
let pathToLastRow = IndexPath.init(row: indexOfLastRow, section: indexOfLastSection)
}
}
我的 Swift 5 實現確保了有效的IndexPath
。
extension UITableView {
/// Returns nil if the table view has no rows.
func kjy_indexPathOfLastRow() -> IndexPath? {
let sectionsCount = numberOfSections
guard sectionsCount > 0 else {
return nil
}
for section in (0..<sectionsCount).reversed() {
let rowsCount = numberOfRows(inSection: section)
if rowsCount > 0 {
return IndexPath(row: rowsCount - 1, section: section)
}
}
return nil
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.