gpt4 book ai didi

ios - 在 cellForRowAt indexPath 方法之外获取选定的行号

转载 作者:行者123 更新时间:2023-11-28 20:48:58 24 4
gpt4 key购买 nike

我需要在 cellForRowAt indexPath 方法之外访问选定的行。我目前正在做的是使用 Int 来跟踪已选择的行并在 cellForRowAt indexPath 中分配该值,以便稍后可以使用 Int 作为当前选中的行。

一切正常,但我想确保没有其他方法可以在不使用 Int 变量的情况下执行此操作。

有没有办法在 cellForRowAt indexPath 方法之外获取选定的行号,而不必使用 Int 变量来跟踪行号?

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
}

最佳答案

你可以尝试使用indexPathForSelectedRow

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

关于ios - 在 cellForRowAt indexPath 方法之外获取选定的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59520154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com