找出最后添加的 UITableViewCell
Find out last added UITableViewCell
我想找出我的 UITableView 的最后一个位置的最后一个 UITableViewCell,并将此单元格的 detailTextLabel 更改为 "Last added"。有人知道如何在 Swift 中执行此操作吗?
由于 table 视图总是需要数据源,因此您应该查看那里而不是 table 视图。
NSIndexPath *lastCellIDP = [NSIndexPath indexPathForRow:self.dataSource.lastObject inSection:0];
UITableViewCell *lastCell = [self.tableView cellForRowAtIndexPath:lastCellIDP];
lastCell.detailTextLabel.text = @"U NEED 2 ChAnGE!";
该代码假设您的数据源是 NSArray
类型。
就是说,当您绑定数据时,我会将此逻辑放在 UITableViewCell
子类中,或者如果它是最后一项,则可能会放在数据源模型中的 属性 中。
tableView 为您提供了创建最后一行的 indexPath 并获取它的单元格所需的一切。
let lastSectionIndex = tableView.numberOfSections()-1
let lastSectionLastRow = tableView.numberOfRowsInSection(lastSectionIndex) - 1
let indexPath = NSIndexPath(forRow:lastSectionLastRow, inSection: lastSectionIndex)
let cell = tableView.cellForRowAtIndexPath(indexPath)
但根据您的需要,您可能更好地访问 tableView 的委托中的单元格
func tableView(_ tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
类似
let lastSectionIndex = tableView.numberOfSections()-1
let lastSectionLastRow = tableView.numberOfRowsInSection(lastSectionIndex) - 1
let lastIndexPath = NSIndexPath(forRow:lastSectionLastRow, inSection: lastSectionIndex)
let cellIndexPath = tableView.indexPathForCell(cell)
if cellIndexPath == lastIndexPath {
// the last cell is about to be displayed. change get here.
}
我想找出我的 UITableView 的最后一个位置的最后一个 UITableViewCell,并将此单元格的 detailTextLabel 更改为 "Last added"。有人知道如何在 Swift 中执行此操作吗?
由于 table 视图总是需要数据源,因此您应该查看那里而不是 table 视图。
NSIndexPath *lastCellIDP = [NSIndexPath indexPathForRow:self.dataSource.lastObject inSection:0];
UITableViewCell *lastCell = [self.tableView cellForRowAtIndexPath:lastCellIDP];
lastCell.detailTextLabel.text = @"U NEED 2 ChAnGE!";
该代码假设您的数据源是 NSArray
类型。
就是说,当您绑定数据时,我会将此逻辑放在 UITableViewCell
子类中,或者如果它是最后一项,则可能会放在数据源模型中的 属性 中。
tableView 为您提供了创建最后一行的 indexPath 并获取它的单元格所需的一切。
let lastSectionIndex = tableView.numberOfSections()-1
let lastSectionLastRow = tableView.numberOfRowsInSection(lastSectionIndex) - 1
let indexPath = NSIndexPath(forRow:lastSectionLastRow, inSection: lastSectionIndex)
let cell = tableView.cellForRowAtIndexPath(indexPath)
但根据您的需要,您可能更好地访问 tableView 的委托中的单元格
func tableView(_ tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
类似
let lastSectionIndex = tableView.numberOfSections()-1
let lastSectionLastRow = tableView.numberOfRowsInSection(lastSectionIndex) - 1
let lastIndexPath = NSIndexPath(forRow:lastSectionLastRow, inSection: lastSectionIndex)
let cellIndexPath = tableView.indexPathForCell(cell)
if cellIndexPath == lastIndexPath {
// the last cell is about to be displayed. change get here.
}