[英]How to pass TableViewCell value into new ViewController in Swift 3.0?
[英]Swift : Pass TableViewCell text to new ViewController's cell
如何使用填充在tableVC中的數組將相同的文本從tableVC傳遞到詳細信息tableVC。
它在tableVC中工作,但沒有數據傳遞到detailVC中。
他們共享一個tableviewcell。
class TableViewCell: UITableViewCell {
@IBOutlet var img: UIImageView!
@IBOutlet var title: UILabel!
}
tableVC
class TableViewController: UITableViewController {
var thecourseName = [String]()
override func viewDidLoad() {
super.viewDidLoad()
thecourseName = ["course 1 ","course 2 ","course 3 "]
theimg = [UIImage(named: "109.png")!,UIImage(named: "110.png")!]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.title.text = thecourseName[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if ( segue == "toInfo") {
var text = self.tableView.indexPathForSelectedRow()
var detailsVC: DetalisTableViewController = segue.destinationViewController as! DetalisTableViewController
detailsVC.courseName = thecourseName
}
}
detailVC
import UIKit
class DetalisTableViewController: UITableViewController {
var courseName = [String]()
@IBOutlet var passedCourse: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.title.text = courseName[indexPath.row]
return cell
}
您的問題是您沒有將有關所選單元格內容的任何信息傳遞給detailViewController。 改為這樣做:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if ( segue.identifier == "toInfo") {
if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
var detailsVC = segue.destinationViewController as! DetalisTableViewController
println(thecourseName[indexPath.row])
println(indexPath.row)
detailsVC.courseName = thecourseName[indexPath.row]
}
}
}
現在,無需將整個課程名稱數組傳遞給DetailsTableViewController
,您的變量courseName
僅包含一個:
var courseName: String = ""
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
cell.title.text = courseName
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.