gpt4 book ai didi

swift - 从行中的单元格获取标签文本

转载 作者:行者123 更新时间:2023-11-30 11:32:57 25 4
gpt4 key购买 nike

当我调用 did select row 时,是否有另一种方法可以从 refinementslabel.text 获取字符串,因为我认为/显然是在新的 XCode 调用中
let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as!服务单元

使我的应用程序崩溃

所以我正在寻找类似的代码
让文本=在indexpath.row处的refinementsLabel.text

但我不确定到底要输入什么?

let sections = ["All Users & Services","Automotive", "Building & Construction", "Cleaning", "Landscaping & Gardening"]

let services = [["All Users & Services"],["Automotive"],["Air Conditioning & Heating","Bricklaying", "Carpentry","Carpet Layer","Concreting & Paving","Electrical","Fencing and Gates","Flooring","Handyman","Other","Painting & Decorating","Pet Control","Plastering","Plumbing","Roofing","Rubbish Removal","Scaffolding","Tiling"], ["Cleaning"],["Landscaping & Gardening"]]

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ServiceCell
var service = serviceTypeCell.refinementsLabel.text
service = services[indexPath.section][indexPath.row]

defaults.set(service, forKey: "Service being searched")
guard let serviceBeingSearched = self.defaults.string(forKey: "Service being searched") else { return }
navigationItem.title = serviceBeingSearched
tableView.reloadData()

}

最佳答案

创建新单元格时使用以下方法,如果您使用该方法而不是应用程序崩溃,则在 cellforRow 中使用该方法

let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ServiceCell  

如果您在 didselect 方法中获取单元格,则可以使用以下代码

let cell = tableView.cellForRow(at: indexPath) as? ServiceCell

如果您从中获取字符串,则可以选择单元格而不是使用下面的行

let service = services[indexPath.section][indexPath.row]

关于swift - 从行中的单元格获取标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062033/

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