Swift:当使用静态 table 时,select 行在索引路径 returns 为零
Swift: Did select row at index path returns nil when using a static table
我正在我的应用程序中构建一个设置页面,因此使用静态单元而不是动态单元是有意义的,因为我从不在此视图中动态生成任何内容。
我采用了将 table 分成多个组的模式,每个类别即用户首选项、设置等
我尝试使用 UITableView
s 委托方法 didSelectRowAtIndexPath
但是在调试时我似乎总是为我的单元格传递一个空值。我需要做的是从标签中提取文本来描述没有 segue 的单元格的操作(例如注销和删除帐户,这些只是提供一个确认模式,然后将处理该操作)
我的didSelectRowAtIndexPath
方法:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell != nil {
// Set the CellID
var cellID: AnyObject! = (cell?.textLabel!.text == nil) ? "" : cell?.textLabel?.text
// The values are hardcoded so we can switch with confidence
switch cellID as String {
case "Logout":
logout()
default:
println("un-managed action \(cellID.localizedDescription) and \(cellID) index path: \(indexPath)")
}
}
}
为什么我总是打印 nil
结果?我唯一的猜测是它在所有可用部分中查找索引 0,并且不能假设我也指向哪个单元格,因此 returns 一个 nil
对象,一些澄清和解决方案会很棒。
如您的评论所述,您从未专门设置单元格的 textLabel
属性;您只在界面中的单元格中添加了 UILabel
。假设每个单元格只添加了一个标签,您可以遍历单元格的子视图并通过查找 UILabel
子视图动态访问标签,例如:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell != nil {
// Set the CellID
var label:UILabel = UILabel()
for subview in self.view.subviews {
if subview is UILabel {
label = subview as UILabel
break
}
}
var cellID: AnyObject! = (label.text == nil) ? "" : label.text
// The values are hardcoded so we can switch with confidence
switch cellID as String {
case "Logout":
logout()
default:
println("un-managed action \(cellID.localizedDescription) and \(cellID) index path: \(indexPath)")
}
}
}
如果您的单元格中有多个标签,您可以为每个标签添加一个标签,然后在 for 循环中检查标签。
我正在我的应用程序中构建一个设置页面,因此使用静态单元而不是动态单元是有意义的,因为我从不在此视图中动态生成任何内容。
我采用了将 table 分成多个组的模式,每个类别即用户首选项、设置等
我尝试使用 UITableView
s 委托方法 didSelectRowAtIndexPath
但是在调试时我似乎总是为我的单元格传递一个空值。我需要做的是从标签中提取文本来描述没有 segue 的单元格的操作(例如注销和删除帐户,这些只是提供一个确认模式,然后将处理该操作)
我的didSelectRowAtIndexPath
方法:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell != nil {
// Set the CellID
var cellID: AnyObject! = (cell?.textLabel!.text == nil) ? "" : cell?.textLabel?.text
// The values are hardcoded so we can switch with confidence
switch cellID as String {
case "Logout":
logout()
default:
println("un-managed action \(cellID.localizedDescription) and \(cellID) index path: \(indexPath)")
}
}
}
为什么我总是打印 nil
结果?我唯一的猜测是它在所有可用部分中查找索引 0,并且不能假设我也指向哪个单元格,因此 returns 一个 nil
对象,一些澄清和解决方案会很棒。
如您的评论所述,您从未专门设置单元格的 textLabel
属性;您只在界面中的单元格中添加了 UILabel
。假设每个单元格只添加了一个标签,您可以遍历单元格的子视图并通过查找 UILabel
子视图动态访问标签,例如:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell != nil {
// Set the CellID
var label:UILabel = UILabel()
for subview in self.view.subviews {
if subview is UILabel {
label = subview as UILabel
break
}
}
var cellID: AnyObject! = (label.text == nil) ? "" : label.text
// The values are hardcoded so we can switch with confidence
switch cellID as String {
case "Logout":
logout()
default:
println("un-managed action \(cellID.localizedDescription) and \(cellID) index path: \(indexPath)")
}
}
}
如果您的单元格中有多个标签,您可以为每个标签添加一个标签,然后在 for 循环中检查标签。