【发布时间】:2019-02-13 18:32:59
【问题描述】:
我的场景,我正在尝试创建tableview 单元格单击以获取label 数据,我还有更多values,但我不想在label 中显示。现在,我怎样才能将assignunshowed 数据转换为cell,因为我在sending 单元格didselect 之后将值转换为另一个view controller。
例如:
我有两个数组值
let alpha: [String] = ["A", "B", "C", "D", "E"]
let numeric: [String] = ["1", "2", "3", "4", "5"]
在这里,我只将 alpha 值播种到 tableview 单元格中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.textlabel.text = alpha[indexPath.row]
return cell
}
现在,我要获取 tableview 单元格选择的值,在这里,我可以获取 cell.textlabel.text 值,但我还需要获取数值而不分配任何标签。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = tableView.cellForRow(at: indexPath)! as? MyCustomCell
print(currentCell?.textlabel?.text ?? "unknown alpha")
// Here I need to get numeric values also
}
【问题讨论】:
-
didSelectRowAt中的参数与cellForRowAt中的参数相同:indexPath...let numbericValue = numeric[indexPath.row] -
“坚持”是什么意思?您始终可以使用
numeric[indexPath.row]从您的 dataSource 数组中获取它,就像 Robert 写的那样