【问题标题】:How to uniquely identify each cell in UITableView如何唯一标识 UITableView 中的每个单元格
【发布时间】:2018-09-27 05:15:20
【问题描述】:

我有一个UITableView,我正在尝试与另一个viewcontroller 进行segue,我需要行号以便我可以从数组中选择一个字符串,以便我可以在下一个视图控制器中显示这个字符串,我现在有这个代码。

let tableFrontView = segue.destination as! FCTableFrontViewController
tableFrontView.frontText = path[FlashCardsTableViewCell.init().tag].flashCardFront

FlashCardsTableViewCell.init().tag 目前正在返回一个 int 用于测试目的,但我想知道我可以用什么替换它以获得用户选择的行号。 谢谢

【问题讨论】:

  • UITableViewdidSelectRowAt 方法,您可以在其中使用 let strSelectedValue = arrData[indexPath.row] 从数组中获取选定的字符串

标签: ios arrays swift uitableview segue


【解决方案1】:

你可以这样试试:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let tableFrontView = segue.destination as! FCTableFrontViewController
    let selectedIndexPath = tblView.indexPathForSelectedRow
    let selectedRow = (selectedIndexPath?.row)!
    print(selectedRow)
}

【讨论】:

    【解决方案2】:

    我假设你正在编写prepare(for:)中显示的代码sn-p?

    如果是这种情况,请转到执行 segue 的位置,这可能在 didSelectedRowAtIndexPath 委托方法中。如果你没有这样的方法,你应该实现它。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showTableFrontVC", sender: nil)
    }
    

    现在将您传递的任何内容替换为sender,并将其替换为indexPath.row

    performSegue(withIdentifier: "showTableFrontVC", sender: indexPath.row)
    

    现在在prepare(for:),您可以将发件人解包为Int

    let tableFrontView = segue.destination as! FCTableFrontViewController
    let rowSelected = sender as! Int
    tableFrontView.frontText = path[rowSelected].flashCardFront
    

    【讨论】:

      【解决方案3】:

      使用 TableView 的委托方法:

       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       // indetify cell using indexPath.row attribute
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-04
        • 1970-01-01
        相关资源
        最近更新 更多