代码之家  ›  专栏  ›  技术社区  ›  fs_tigre

在cellForRowAt indexPath方法外部获取所选行号

  •  0
  • fs_tigre  · 技术社区  · 5 年前

    我需要访问 cellForRowAt indexPath 方法。我现在所做的是使用 Int 要跟踪选定的行并在 cellForRowAt索引 所以我以后可以用 内景 作为当前选定的行。

    一切都很好,但我想确保没有其他方法可以不使用 内景 变量。

    是否有方法在 cellForRowAt索引 方法而不必使用 内景 用于跟踪行号的变量?

    var selectedRow:Int?
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        selectedRow = indexPath.row
    }
    
    func myFunction(){
        let indexPath = IndexPath(row:selectedRow!, section: 1)
        let cell = self.myTable.cellForRow(at: indexPath) as! CustomCell
    }
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   Shehata Gamal    5 年前

    你可以试着用 indexPathForSelectedRow

    guard let index =  self.myTable.indexPathForSelectedRow ,  
          let cell = self.myTable.cellForRow(at: index) as? CustomCell 
    else { return }