UITableView Detecting Last Cell

前端 未结 8 740
广开言路
广开言路 2021-01-31 10:46

How can I detect when a UITableView has been scrolled to the bottom so that the last cell is visible?

相关标签:
8条回答
  • 2021-01-31 11:22
    - (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
        }
    }
    
    0 讨论(0)
  • 2021-01-31 11:24

    I had some problems with the willDisplayCell method. This works for me:

    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")
        }
    }
    

    Just pass this in the delegate methods, and change 'table' to your tableView.

    0 讨论(0)
  • 2021-01-31 11:25

    For Xcode 9 and Swift 4/5

    //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
        }
    }
    
    0 讨论(0)
  • 2021-01-31 11:26

    Create elegant extension for 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
        }
    }
    

    Example of usage:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        if tableView.isLast(for: indexPath) {
            //do anything then
        }
    
        return cell
    }
    
    0 讨论(0)
  • 2021-01-31 11:26

    For Xcode 8 and swift 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")
            }
        }
    
    0 讨论(0)
  • 2021-01-31 11:38

    Implement the tableView:willDisplayCell:forRowAtIndexPath: method in your UITableViewDelegate and check to see if it's the last row.

    0 讨论(0)
提交回复
热议问题