gpt4 book ai didi

ios - 在 tableView.deselectRowAtIndexPath 中使用 indexPath 与 indexPath.row

转载 作者:行者123 更新时间:2023-11-28 13:16:12 60 4
gpt4 key购买 nike

我注意到要禁用表格单元格的突出显示功能,您可以使用以下方法:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
...
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}

我不明白的是第一个参数 indexPath 中发生了什么。在教学书中我一直在阅读几乎所有其他方法一直使用 indexPath.row 来选择单个单元格:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell

cell.nameLabel.text = restaurantNames[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
}

出于好奇,我尝试将 indexPath.row 作为参数传递,但出现错误,'NSNumber' 不是 'NSIndexPath' 的子类型。在这种特殊情况下,使用 indexPathindexPath.row 的主要区别是什么。

最佳答案

来自NSIndexPath类(class)引用:

The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path.

TableView (和 Collection View )使用 NSIndexPath 来索引它们的项目因为它们是嵌套结构(部分和行)。第一个索引是 TableView 的部分,第二个索引部分中的

indexPath.sectionindexPath.row 只是一个“方便”的属性,你总是有

indexPath.section == indexPath.indexAtPosition(0)
indexPath.row == indexPath.indexAtPosition(1)

它们记录在 NSIndexPath UIKit Additions 中.

所以 NSIndexPath 是一个 Objective-C 类并且被所有 TableView 使用功能。 sectionrow 属性是 NSInteger 数字。(因此,如果需要 NSIndexPath,则不能传递 indexPath.row。)

对于没有部分的 TableView (UITableViewStyle.Plain)section 始终为零,indexPath.row 是行号一个项目(在唯一的部分)。在这种情况下,您可以使用indexPath.row 索引数组作为 TableView 数据源:

cell.nameLabel.text = restaurantNames[indexPath.row]

对于包含部分 (UITableViewStyle.Grouped) 的 TableView ,您有同时使用 indexPath.sectionindexPath.row(如示例:What would be a good data structure for UITableView in grouped mode ).


一种更好的方法来禁用特定 TableView 行的选择是覆盖 willSelectRowAtIndexPath 委托(delegate)方法:

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

// return `indexPath` if selecting the row is permitted
// return `nil` otherwise
}

此外,要禁用触摸时单元格突出显示的外观,您还可以设置

cell.selectionStyle = .None

创建单元格时。所有这些(例如)在 UITableview: How to Disable Selection for Some Rows but Not Others .

关于ios - 在 tableView.deselectRowAtIndexPath 中使用 indexPath 与 indexPath.row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28927404/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com