Получить последнюю ячейку в разделе UITableview
У меня есть раздел в UITableView, который имеет несколько строк. Я хочу, чтобы последняя строка или последняя ячейка секции добавили в нее индикатор раскрытия.
Один из способов, который я собираюсь использовать, - это:
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:2]-1 inSection:2];
Есть ли другой лучший способ получить ячейку или индексный путь последней ячейки в разделе?
Ответы
Ответ 1
NSInteger totalRow = [tableView numberOfRowsInSection:indexPath.section];//first get total rows in that section by current indexPath.
if(indexPath.row == totalRow -1){
//this is the last row in section.
}
надеюсь, что это поможет.
Я сделал это в tableView: метод willDisplayCell: forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
Ответ 2
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//Section 0
if (indexPath.section == 0) {
// Is Last row?
if ([dataSouceArray count] == (indexPath.row+1)) {
//Yes
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
else{
// other rows
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
Ответ 3
Вы должны сделать это в своем методе cellForRowAtIndexPath
. Вы можете легко обнаружить, к какому участку принадлежит эта ячейка, и последнему.
Ответ 4
Вы можете получить его из своего источника данных, который вы установили в методах делегатов,
источник данных, который вы назначаете в
numberOfRowsInSection
.
[arrayStores lastObject];
поэтому в cellForRowAtIndexPath
вы можете легко проверить его,
Ответ 5
Быстрая версия:
var totalRow = tableView.numberOfRows(inSection: indexPath.section)
//first get total rows in that section by current indexPath.
if indexPath.row == totalRow - 1 {
//this is the last row in section.
}
Ответ 6
Интересно, как я это пропустил. Самое простое решение здесь.
Я написал в методе didSelectRowAtIndexPath в соответствии с моим требованием.
if (indexPath.row ==userAccountsList.count-1)
{
NSLog(@"last row it is ");
}
В приведенном выше коде userAccountsList - это массив, который мы передаем в tableview.