2015-03-20 1 views
1

У меня есть табличное представление, которое использует пользовательские ячейки. Проблема в том, что я не знаю, как передать значение textField в моей настраиваемой ячейке на следующий контроллер представления с помощью prepareForSegue. Код, который я использую:Проблема с передачей пользовательского значения ячейки в новый viewController в swift

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 


    } 

Любые идеи? Большое спасибо

ответ

-1

Вы получаете выбранную ячейку через 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-перетаскивание SEGUE из ячейки в 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 
    } 
} 
+0

Второе решение сделало трюк. Большое спасибо. Очень ценю вашу помощь –