【发布时间】:2017-06-12 18:21:08
【问题描述】:
我想做简单的UITabelView。我使用名为 backgroundviewcell 的自定义类注册了它的单元格。我想获取所选单元格的标签。我尝试了很多次,但输出是零值。我也尝试过堆栈溢出的解决方案,但它对我不起作用。这是我的 cellForRowAt indexPath 代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell
let imageNames = sections[indexPath.section].images[indexPath.row]
cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
//cell.detailTextLabel?.textColor = UIColor.red
//cell.isHighlighted = false
cell.backgroundColor = UIColor.clear
cell.iconview.image = UIImage(named: imageNames)
return cell
}
这是我的 didSelectRowAt indexPath 代码:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)!
celltext = currentCell.textLabel!.text
performSegue(withIdentifier: "showPlayer", sender: self)
}
我的 Segue 方法是:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlayer" {
let playerVC = segue.destination as! PlayerViewController
playerVC.trackName = (celltext)! as String
}
}
【问题讨论】:
-
你不应该。您应该从您的 tableview 数据源中获取它
-
使用
indexPathForSelectedRow访问您的数组元素 -
请使用 lowerCamelCase 中的所有 var - 因为所有类始终使用 UpperCamelCase
-
请在 cellForRowAtIndexPath 方法中向我们展示您的代码,并告诉我们您为什么要从 Cell 获取 Label。
-
感谢@muesha 的建议。
标签: arrays swift uitableview