iOS - Как я могу получить IndexPath последнего элемента в табличном представлении?

Это вызывается конечными символами строки DOS/Windows. Как сказанный Andy Whitfield, команда dos2unix Unix поможет решить проблему. Если Вы хотите больше информации, можно прочитать страницы справочника для той команды.

25
задан EmptyStack 30 August 2011 в 10:25
поделиться

5 ответов

Вы можете получить 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];
}
30
ответ дан 28 November 2019 в 17:40
поделиться

В быстром 3

let lastRowIndex = tableView.numberOfRows(inSection: tableView.numberOfSections-1)

if (indexPath.row == lastRowIndex - 1) {
     print("last row selected")
}
10
ответ дан 28 November 2019 в 17:40
поделиться

попробуйте это:

В cellForRowAtIndexPath

if(indexPath.row == myArray.count -1)

{
     myIndexPath = indexpath;
}

myIndexPath должен быть объектом NSIndexPath

8
ответ дан 28 November 2019 в 17:40
поделиться

Большинство ответов здесь, включая принятый ответ, не учитывают действительные случаи табличного представления с нулевыми разделами или заключительного раздела с нулевыми строками.

Лучший способ представить эти ситуации - использовать индекс 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;
}
4
ответ дан 28 November 2019 в 17:40
поделиться

Попробуйте этот код:

//   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)
    }
}
0
ответ дан 28 November 2019 в 17:40
поделиться