【问题标题】:Set String Identifier to Cell in Swift在 Swift 中将字符串标识符设置为单元格
【发布时间】:2022-01-09 03:04:39
【问题描述】:

我有一个接收字符串标识符数组的 TableView,我需要为我拥有的每个单元格设置一个标识符并将其关联到一个名为“cellPressedId”的变量,但是当我这样做时,我只是获取数组的最后一个值

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let selectedCell = tableView.cellForRow(at: indexPath) {
            if let selectedCell = tableView.cellForRow(at: indexPath) {
                var cellPressed = self.cellPressedId
            }
        }

我需要做的是为每个单元格设置这个标识符,当我按下这个单元格时获取标识符

【问题讨论】:

  • 在某些时候,你不做let identifier = myArray[indexPath.row] in tableView(_/cellForRowAt:)`吗?设置标识符?为什么不在这里做同样的事情?

标签: swift tableview cell


【解决方案1】:

当一个单元格被选中时,你可以通过这个函数访问所选单元格的 indexPath: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 而且我确定您可以访问您的数据数组(您提到的字符串数组),您可以访问这样的选定元素:

let selectedItem = myArray[indexPath.row]

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多