无法从UITableViewCell获取Label值

时间:2017-05-16 13:32:03

标签: ios swift uitableview uilabel didselectrowatindexpath

我试图将值从一个视图控制器传递到另一个视图控制器。正如我测试的那样,这可以运行我的代码。但是当我试图获取细胞标签价值时,我遇到了问题。我尝试了在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 ?? "")
        }
    }

}

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

用下面给出的

替换你的didselect方法
curl -v https://testbed-epp.nominet.org.uk:700

答案 2 :(得分:0)

您当前的代码中存在两个基本问题,首先是您将单元格转换为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