Swift:将 TableViewCell 文本传递到新 ViewController 的单元格
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!
}
表VC
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
}
}
详情VC
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]
}
}
}
现在,您的变量 courseName
只包含一个:
,而不是将整个课程名称数组传递给 DetailsTableViewController
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
}
如何使用在 tableVC 中填充的数组将相同的文本从 tableVC 传递到详细信息 tableVC。
它在 tableVC 中工作,但没有数据传递到 detailVC。
他们共享一个 tableviewcell。
class TableViewCell: UITableViewCell {
@IBOutlet var img: UIImageView!
@IBOutlet var title: UILabel!
}
表VC
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
}
}
详情VC
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]
}
}
}
现在,您的变量 courseName
只包含一个:
DetailsTableViewController
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
}