gpt4 book ai didi

ios - 无法从 UITableViewCell 获取标签值

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:23 24 4
gpt4 key购买 nike

我正在尝试将一个值从一个 View Controller 传递到另一个 View Controller 。正如我测试的那样,这没问题并且可以使用我的代码运行。但是当我尝试获取单元格标签值时遇到问题。我尝试了在 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 ?? "")
}
}

}

最佳答案

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

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

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

关于ios - 无法从 UITableViewCell 获取标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002969/

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