- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我注意到要禁用表格单元格的突出显示功能,您可以使用以下方法:
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' 的子类型
。在这种特殊情况下,使用 indexPath
和 indexPath.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.section
和 indexPath.row
只是一个“方便”的属性,你总是有
indexPath.section == indexPath.indexAtPosition(0)
indexPath.row == indexPath.indexAtPosition(1)
它们记录在 NSIndexPath UIKit Additions 中.
所以 NSIndexPath
是一个 Objective-C 类并且被所有 TableView 使用功能。 section
和 row
属性是 NSInteger
数字。(因此,如果需要 NSIndexPath
,则不能传递 indexPath.row
。)
对于没有部分的 TableView (UITableViewStyle.Plain
)section 始终为零,indexPath.row
是行号一个项目(在唯一的部分)。在这种情况下,您可以使用indexPath.row
索引数组作为 TableView 数据源:
cell.nameLabel.text = restaurantNames[indexPath.row]
对于包含部分 (UITableViewStyle.Grouped
) 的 TableView ,您有同时使用 indexPath.section
和 indexPath.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/
我正在将 UITableViewCell 的 detailTextLabel 设置为 highlighted:YES; - (void)tableView:(UITableView *)tableVi
我正在学习本教程:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTut
我一直在编写一个小型 Swift 应用程序。我现在正在使用 tableview 并尝试选择一行,更改图像并取消选择该行。 我的自定义 UITableViewCell 类如下所示: class Sele
我有一个允许多项选择的表格 View 。返回 tableView 时,我想清除所有以前的选择。下面的代码仅清除第一个选定的单元格。如何清除所有单元格? override func viewWillAp
当返回到 -viewWillAppear:animated 中的 TableView 时,我使用以下代码取消选择选定的 TableView 单元格。 [self.tableView deselectR
在我的表格 View 中,有许多行具有披露详细信息按钮附件类型,当按下该行或附件时,一个新 View 将被推送到导航 Controller 。我在 didSelectRowAtIndexPath 函数
我有一个表格 View ,其中包含一堆各种漂亮颜色的单元格。当我点击一个时,它突出显示很好,但是当我调用 deselectRowsAtIndexPaths 时,淡出动画从突出显示的颜色变为白色,而不是
有什么区别 [tableView deselectRowAtIndexPath:indexPath animated:NO]; 和 UITableViewCell *cell = [tableVie
我正在处理带有自定义单元格的表格 View 。我需要通过为标题标签设置背景颜色来突出显示当前选定的(事件的)单元格,这是 cell.contentView 的直接 subview 。我的代码逻辑是这样
我注意到要禁用表格单元格的突出显示功能,您可以使用以下方法: override func tableView(tableView: UITableView, didSelectRowAtIndexPa
我有一个 UITableView,其中的单元格在被选中时将 viewController 插入堆栈。 subview Controller 接受用户输入,然后从堆栈中弹出。 当弹出子 viewCont
我是一名优秀的程序员,十分优秀!