UITableView Обнаружение последней ячейки - PullRequest
17 голосов
/ 14 июля 2010

Как я могу определить, когда UITableView прокручивается вниз, чтобы была видна последняя ячейка?

Ответы [ 7 ]

40 голосов
/ 14 июля 2010

Внутри 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
    }

    ...

}
23 голосов
/ 14 июля 2010

Реализуйте метод tableView:willDisplayCell:forRowAtIndexPath: в вашем UITableViewDelegate и проверьте, не последний ли это ряд.

14 голосов
/ 29 марта 2014
- (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
    }
}
5 голосов
/ 10 мая 2017

Создать элегантное расширение для 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
}
3 голосов
/ 28 февраля 2017

Для Xcode 9 и 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
    }
}
1 голос
/ 22 февраля 2017

Для Xcode 8 и 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 голосов
/ 26 октября 2017

У меня были некоторые проблемы с методом 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.

...