【问题标题】:Can't get Label value from UITableViewCell无法从 UITableViewCell 获取标签值
【发布时间】:2017-05-16 13:32:02
【问题描述】:

我正在尝试将值从一个视图控制器传递到另一个视图控制器。正如我测试的那样,这没问题并且可以使用我的代码运行。但是当我尝试获取单元格标签值时遇到问题。我尝试了在 google 和 stackoverflow 上找到的所有内容,但对此没有答案。我在代码上添加了一条注释,上面写着// I THINK THE PROBLEM IS ON THE NEXT LINE OF CODE,以帮助您找到我卡在哪里。

我的代码:

import UIKit


class PlacesTableViewController: UITableViewController {

@IBOutlet weak var placesTableView: UITableView!

var places = [Places]()
var valueToPass:String!
let cellIdentifier = "PlacesTableViewCell"

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        // Table view cells are reused and should be dequeued using a cell identifier.


        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell  else {
            fatalError("The dequeued cell is not an instance of PlacesTableView Cell.")
        }

        let place = places[indexPath.row]

        cell.placeLabel.text = place.name
        cell.ratingControl.rating = place.rating

        return cell

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        print("You selected cell #\(indexPath.item)!")


        let indexPath = placesTableView.indexPathForSelectedRow

        // I THINK THE PROBLEM IS ON THE NEXT LINE OF CODE
        let currentCell = placesTableView.cellForRow(at: indexPath!)! as UITableViewCell

        print(currentCell.detailTextLabel?.text ?? "Failed to use Cell Label")
        valueToPass = currentCell.detailTextLabel?.text ?? "Failed to use Cell Label"
        performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self)

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "ShowCommentsTableViewController") {
            // initialize new view controller and cast it as your view controller
            let viewController = segue.destination as! CommentsTableViewController
            // will be stored in passedValue on the CommentsTableViewController
            viewController.passedValue = valueToPass
            //print (viewController.passedValue ?? "")
        }
    }

}

【问题讨论】:

  • 使用数据源,而不是单元格。另外,您使用的是PlacesTableViewCell,而不是经典的UITableViewCell,并且您使用placeLabel 作为UILabel,而不是detailTextLabel
  • 你应该只使用places数组来获取数据,你已经在didSelect方法中有indexPath。
  • 提示 #1:遵循 Scriptable 的答案... 提示 #2:不要“尝试在 google 和 stackoverflow 上找到的所有内容” -- 阅读了解你发现了什么,然后实施它。如果您不理解,复制/粘贴代码将无济于事。
  • @DonMag 是的,你是对的!

标签: ios swift uitableview uilabel didselectrowatindexpath


【解决方案1】:

您为什么不像在 cellForRowAt 中那样使用数据存储中的原始值?

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

    let place = places[indexPath.row]
    valueToPass = place.name
    performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self)
}

【讨论】:

  • 建议对我的帖子进行编辑以将其更改为 places[(indexPath?.row)!] 我拒绝了。 indexPath 不是可选的
  • 但是我的编译器说要改变它,因为它无法构建。在我进行更改后,代码正在运行,它发生了我想要的。谢谢你的回答。
  • 好的,我会仔细检查并在需要时更新。上面的函数定义没有显示可选。更新:对我来说在 xcode 中很好
【解决方案2】:

用下面给出的替换你的 didselect 方法

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

    let place = places[indexPath.row]

    valueToPass = place.name
    performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self)

}

【讨论】:

  • 谢谢!但@Scriptable 是第一个。
【解决方案3】:

您当前的代码中有两个基本问题,首先您将单元格转换为UITableViewCell 而不是PlacesTableViewCell,第二个问题是您正在访问detailTextLabel,这在您的自定义单元格中不可用,而不是使用placeLabel。正确的代码应该是这样的:

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

{
    print("You selected cell #\(indexPath.item)!")


    let indexPath = placesTableView.indexPathForSelectedRow

    // I THINK THE PROBLEM IS ON THE NEXT LINE OF CODE
    let currentCell = placesTableView.cellForRow(at: indexPath!)! as PlacesTableViewCell

    print(currentCell. placeLabel?.text ?? "Failed to use Cell Label")
    valueToPass = currentCell. placeLabel?.text ?? "Failed to use Cell Label"
    performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self)

}

如果你不想经历这个麻烦,你可以像这样简单地从原始数组中获取值:

let place = places[indexPath.row]
valueToPass = place.name

【讨论】:

  • 有人想告诉我为什么我的回答有反对票吗?
猜你喜欢
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多