Ios 如何将数据从UITableViewCell传递到ViewController?
我已经学习了多个教程,并在这里尝试了许多答案,但我似乎无法找出问题所在 这是我的密码:Ios 如何将数据从UITableViewCell传递到ViewController?,ios,swift,uitableview,parse-platform,viewcontroller,Ios,Swift,Uitableview,Parse Platform,Viewcontroller,我已经学习了多个教程,并在这里尝试了许多答案,但我似乎无法找出问题所在 这是我的密码: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { println("You selected cell #\(indexPath.row)!") let indexPath = homeTimelineTableView.indexPathFor
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
let indexPath = homeTimelineTableView.indexPathForSelectedRow()
let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell
productTitle = currentCell.productCellTitleLabel!.text
println("The product title is \(productTitle)")
self.performSegueWithIdentifier("timelineDetail", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var titleLables: String!
if (segue.identifier == "timelineDetail") {
println("it works thus far!")
let viewController = segue.destinationViewController as! ProductDetailViewController
let selectedRow = homeTimelineTableView.indexPathForSelectedRow()!.row
viewController.titleLabel?.text = productTitle
}else {
println("wtf is wrong with your code?!")
}
}
我没有收到任何错误。但是,当我运行我的应用程序时,我的产品标题仍然拒绝传递给viewcontroller。
有什么建议吗?谢谢。在此阶段
viewController.titleLabel
为零(您正在使用?
,这就是没有错误的原因)
正确的方法应该是:
- 添加
var-productTitle:String
- 不将productTitle传递给UILabel,而是传递给
viewController.productTitle
- 在ProductDetailViewController集合的
中viewDidLoad
viewController.titleLabel?.text=self.productTitle
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var titleLables: String!
if (segue.identifier == "timelineDetail") {
println("it works thus far!")
let viewController = segue.destinationViewController as! ProductDetailViewController
let selectedRow = homeTimelineTableView.indexPathForSelectedRow()!.row
let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell
productTitle = currentCell.productCellTitleLabel!.text
viewController.titleLabel?.text = productTitle
} else {
println("wtf is wrong with your code?!")
}
}
问题主要表现在以下两方面:
let indexPath = homeTimelineTableView.indexPathForSelectedRow()
let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell
首先,func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath)
已经提供了所选的indepath。不需要让indexath=…
其次,不要加载单元格的值(如果单元格可见并且tableView.visibleCells()
会带来其他问题,则返回nil),而是使用数据源在选定的位置获取项目
总的来说,它可能看起来像:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// `indexPath` comes from the function parameters
let item = model.itemAtIndexPath(indexPath)
// the 'model'-part should be the same like in `cellForRow:indexPath`
// your mission is only to get the item at the path, not the cell
productTitle = item.title
self.performSegueWithIdentifier("timelineDetail", sender: self)
}
这成功了!要是你知道我想弄明白这件事有多久了就好了。非常感谢!