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

时间:2018-04-27 12:06:21

标签: swift xcode

还有另一种方法,当我调用选择行时,我可以从refinementslabel.text获取字符串,因为我认为/显然在新的XCode调用中 let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ServiceCell

崩溃了我的应用

所以我正在寻找像这样的代码 let text = refinementsLabel.text at indexpath.row

但我不确定要输入什么内容?

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()

}

1 个答案:

答案 0 :(得分:0)

如果使用该方法而不是app崩溃,则在创建新单元时使用以下方法正在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]