我正在Swift編寫一個Xcode程序。我有一個tableview控制器與一些標籤和每個單元格的圖像。他們從第一個視圖控制器獲取數據。到現在爲止還挺好。現在我想讓用戶點擊一個單元格,它打開一個新的控制器,其中包含與單元格相同的標籤和圖像數據。用下面的代碼打開新的控制器,但我不知道如何傳輸數據。如果有人能幫助我,我會如此感激。從tableview傳遞數據到視圖控制器
下面是我傾向於使用以下碼:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "TableView"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableView
let picture = pic[indexPath.row]
cell.label1.text = picture.name1
cell.photoImage.image = picture.photo
cell.label2.text = picture.name2
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell = indexPath.row
self.performSegueWithIdentifier("segue", sender: cell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue"{
var row = tableView.indexPathForSelectedRow
let viewController = segue.destinationViewController as! vc2
}
}
PS:我有從圖像與識別符「SEGUE」的新控制器SEGUE。 PPS:當然,我曾嘗試以下方法:Send data from TableView to DetailView Swift但是當我運行我的程序我得到的信息的錯誤,我的標籤被意外發現零
您是否已將您的標籤連接到ViewController中的IBOutlets? –
是的,單元格中的標籤連接到tableviewcell文件,目標控制器中的標籤連接到他們的控制器 – j3a3n3