2010-07-13 140 views

回答

21

在你的UITableViewDelegate中实现tableView:willDisplayCell:forRowAtIndexPath:方法,并检查它是否是最后一行。

+1

感谢您的提示。我实际上使用这种方法来触发一个事件,它看起来像是因为它会显示“Will”,它发生在它实际显示单元格之前。显示单元格后是否触发了一种方法? – Ward 2010-07-14 01:41:09

+0

唉,没有。你可以通过使用'performSelector:withObject:afterDelay'稍微延迟一点来调用一个你需要的方法。 – 2010-07-14 02:29:13

+0

@ArtGillespie找到了我正在寻找的东西。干杯! – 2017-05-23 12:51:46

37

tableView:cellForRowAtIndexPath:tableView:willDisplayCell:forRowAtIndexPath:这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    ... 

    NSInteger sectionsAmount = [tableView numberOfSections]; 
    NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]]; 
    if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) { 
     // This is the last cell in the table 
    } 

    ... 

} 
+0

你可能会说我是个假人。感谢帮助有时我觉得这些事情比他们更难。 – Ward 2010-07-13 22:16:01

+3

numberOfSections调用数据源numberOfSectionsInTableView和numberOfRowsInSection调用数据源numberOfRowsInSection。在我的实现中它崩溃了,显示了这个执行的递归日志。 – 2012-12-11 06:28:23

14
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSInteger lastSectionIndex = [tableView numberOfSections] - 1; 
    NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1; 
    if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) { 
     // This is the last cell 
    } 
} 
1

对于Xcode的8快捷3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     let lastSectionIndex = tableView.numberOfSections - 1 
     let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1 
     if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex { 
      print("this is the last cell") 
     } 
    } 
2

对于Xcode中8和Swift3

//Show Last Cell (for Table View) 
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{ 
    if indexPath.row == (yourdataArray.count - 1) 
    { 
     print("came to last row") 
    } 
} 

//Show last cell (for Collection View) 
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) 
{ 
    if indexPath.row == (yourdataArray.count - 1) 
    { 
     // Last cell is visible 
    } 
} 
3

创建优雅延伸UITableView

extension UITableView { 

    func isLast(for indexPath: IndexPath) -> Bool { 

     let indexOfLastSection = numberOfSections > 0 ? numberOfSections - 1 : 0 
     let indexOfLastRowInLastSection = numberOfRows(inSection: indexOfLastSection) - 1 

     return indexPath.section == indexOfLastSection && indexPath.row == indexOfLastRowInLastSection 
    } 
} 

使用示例:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if tableView.isLast(for: indexPath) { 
     //do anything then 
    } 

    return cell 
} 
0

有一些问题与willDisplayCell方法。这适用于我:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let y = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height) 
    let relativeHeight = 1 - (table.rowHeight/(scrollView.contentSize.height - scrollView.frame.size.height)) 
    if y >= relativeHeight{ 
     print("last cell is visible") 
    } 
} 

只要在委托方法中传递此值,并将'table'更改为您的tableView。

相关问题