【问题标题】:How to get Label text of UITableView Cell using Swift?如何使用 Swift 获取 UITableView Cell 的标签文本?
【发布时间】:2017-06-12 18:21:08
【问题描述】:

我想做简单的UITabelView。我使用名为 backgroundviewcell 的自定义类注册了它的单元格。我想获取所选单元格的标签。我尝试了很多次,但输出是零值。我也尝试过堆栈溢出的解决方案,但它对我不起作用。这是我的 cellForRowAt indexPath 代码:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell


    let imageNames = sections[indexPath.section].images[indexPath.row]

    cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
    cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
    cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
    //cell.detailTextLabel?.textColor = UIColor.red
    //cell.isHighlighted = false

    cell.backgroundColor = UIColor.clear
    cell.iconview.image = UIImage(named: imageNames)

     return cell
}

这是我的 didSelectRowAt indexPath 代码:

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


    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)!

    celltext = currentCell.textLabel!.text
    performSegue(withIdentifier: "showPlayer", sender: self)
}

我的 Segue 方法是:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPlayer" {

        let playerVC = segue.destination as! PlayerViewController
        playerVC.trackName = (celltext)! as String


    }
}

【问题讨论】:

  • 你不应该。您应该从您的 tableview 数据源中获取它
  • 使用indexPathForSelectedRow 访问您的数组元素
  • 请使用 lowerCamelCase 中的所有 var - 因为所有类始终使用 UpperCamelCase
  • 请在 cellForRowAtIndexPath 方法中向我们展示您的代码,并告诉我们您为什么要从 Cell 获取 Label。
  • 感谢@muesha 的建议。

标签: arrays swift uitableview


【解决方案1】:

您需要访问用于填充tableView's 单元格数据的数组,而不是从单元格的label 访问文本。

因此,您需要使用 UITableViewDelegate 方法 didSelectRowAtindexPath 并通过 tableView 方法访问您正在使用的数组。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    //Access the array that you have used to fill the tableViewCell
    print(yourArray[indexPath.row])
}

注意:一旦确认TableViewdelegate与您的ViewController正确连接,以便在您选择单元格时调用didSelectRowAt方法。

编辑:如果您想传递数据,请尝试这样。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     performSegue(withIdentifier: "showPlayer", sender: indexPath) //Pass indexPath as sender instead of self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPlayer" {

        let playerVC = segue.destination as! PlayerViewController
        let indexPath = sender as! IndexPath
        playerVC.trackName = sections[indexPath.section].items[indexPath.row]       
    }
}

【讨论】:

  • 实际上我有 5 个部分,所有部分都有差异。数组元素的类型(字符串)。这就是为什么我使用单元格的文本标签让下一个视图控制器播放我的声音文件。
  • @Chetan 检查编辑的答案一次以传递数据
  • 非常感谢。此代码正在运行。你救了我的命。 :) @Nirav D
  • 接受你的回答。 :)
【解决方案2】:

这对我有用:

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

    let cell = tableView.cellForRow(at: indexPath) as! YourCellType
    let labelContent = cell.labelToAccess.text
}

第一行创建了您的单元类型(或标准单元,不是我的情况)的实例。

第二个将标签的内容(在本例中为 labelToAccess)保存在一个常量中。

【讨论】:

  • 感谢您的回复。
【解决方案3】:

试试这个 'Swift 4' -

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let currentCellTxt = yourTableView.cellForRow(at: indexPath)! as? YourCustomTableViewCell

    print(currentCellTxt?.lblYourName?.text)   // 'lblYourName' that you defined in your  'YourCustomTableViewCell'

}

【讨论】:

  • 嗯...你在didSelectRowAtIndexPath的函数参数中已经有了indexPath...你为什么还要麻烦再次获取indexPath?我是想投反对票的边界线。
【解决方案4】:

您没有为 textLabel 设置任何文本。因此它返回 nil 值。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "soundElementalcell") as! backgroundViewCell


    let imageNames = sections[indexPath.section].images[indexPath.row]

    cell.Labeltittle.text = sections[indexPath.section].items[indexPath.row]
    cell.Labeltittle.textColor = UIColor(hex : 0x90BA27)
    cell.LabelDetail.text = sections[indexPath.section].detail[indexPath.row]
    //cell.detailTextLabel?.textColor = UIColor.red
    //cell.isHighlighted = false

    cell.backgroundColor = UIColor.clear
    cell.iconview.image = UIImage(named: imageNames)

     return cell
}

In didSelectRow func try to get value from Labeltittle.text not from textLabel.text

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


    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)!

    celltext = currentCell.Labeltittle.text
    performSegue(withIdentifier: "showPlayer", sender: self)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多