无法将自定义单元格值传递给 swift 中的新 viewController
Trouble passing a custom cell value to new viewController in swift
我有一个使用自定义单元格的表格视图。问题是我不知道如何使用 prepareForSegue 将自定义单元格中的 textField 值传递到下一个视图控制器。我使用的代码是:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("StaffCell") as StaffCustomCell!
if cell == nil {
cell = StaffCustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "StaffCell")
}
// Extract values from the PFObject to display in the table cell
cell?.staffNic?.text = object["Nic"] as String!
cell?.staffApellido?.text = object["Apellido"] as String!
var initialThumbnail = UIImage(named: "iboAzul")
cell.staffFoto.image = initialThumbnail
if let thumbnail = object["FotoStaff"] as? PFFile {
cell.staffFoto.file = thumbnail
cell.staffFoto.loadInBackground()
}
return cell
}
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueStaffSeleccionado" {
let detailViewController = segue.destinationViewController.visibleViewController as StaffDetailViewController
// This is the code I have no idea how to write. I need to get a value from the selected customCell
}
有什么想法吗?非常感谢
您通过 tableView.indexPathForSelectedRow
获取选定的单元格。使用该 indexPath,您可以访问单元格:
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueStaffSeleccionado" {
let detailViewController = segue.destinationViewController.visibleViewController as StaffDetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
// path the cell's content to your detailViewController
detailViewController.myProperty = cell.textLabel?.text
}
}
另一种解决方案:如果直接从 tableViewCell 执行转场(通过按住 Ctrl 并从 InterfaceBuilder 中的单元格拖动转场),那么 sender
是单元格:
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
...
if let cell = sender as StaffCustomCell {
// path the cell's content to your detailViewController
detailViewController.myProperty = cell.textLabel?.text
}
}
我有一个使用自定义单元格的表格视图。问题是我不知道如何使用 prepareForSegue 将自定义单元格中的 textField 值传递到下一个视图控制器。我使用的代码是:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("StaffCell") as StaffCustomCell!
if cell == nil {
cell = StaffCustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "StaffCell")
}
// Extract values from the PFObject to display in the table cell
cell?.staffNic?.text = object["Nic"] as String!
cell?.staffApellido?.text = object["Apellido"] as String!
var initialThumbnail = UIImage(named: "iboAzul")
cell.staffFoto.image = initialThumbnail
if let thumbnail = object["FotoStaff"] as? PFFile {
cell.staffFoto.file = thumbnail
cell.staffFoto.loadInBackground()
}
return cell
}
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueStaffSeleccionado" {
let detailViewController = segue.destinationViewController.visibleViewController as StaffDetailViewController
// This is the code I have no idea how to write. I need to get a value from the selected customCell
}
有什么想法吗?非常感谢
您通过 tableView.indexPathForSelectedRow
获取选定的单元格。使用该 indexPath,您可以访问单元格:
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueStaffSeleccionado" {
let detailViewController = segue.destinationViewController.visibleViewController as StaffDetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
// path the cell's content to your detailViewController
detailViewController.myProperty = cell.textLabel?.text
}
}
另一种解决方案:如果直接从 tableViewCell 执行转场(通过按住 Ctrl 并从 InterfaceBuilder 中的单元格拖动转场),那么 sender
是单元格:
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
...
if let cell = sender as StaffCustomCell {
// path the cell's content to your detailViewController
detailViewController.myProperty = cell.textLabel?.text
}
}