ios - swift : Pass TableViewCell text to new ViewController's cell

标签 ios swift segue uitableview

如何使用在 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
}

Storyboard http://s18.postimg.org/mwkw5hf1l/image.png

最佳答案

您的问题是您没有将有关所选单元格内容的任何信息传递给 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]
        }
    }
}

现在不是将整个类(class)名称数组传递给 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
}

关于ios - swift : Pass TableViewCell text to new ViewController's cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063979/

相关文章:

objective-c - 如何切换回已经加载的 UIViewController?

ios - 在 ios 中合并音频文件的问题

objective-c - TableView 不显示自定义单元格

swift - 如果字符串包含 0,则从 tableView 中删除行

swift - 我们应该对 Swift 中的所有 float 使用 CGFloat 吗?

ios - 使用 Swift 使用 Segue 发送数据

ios - 准备(对于 segue : UIStoryboardSegue, 发件人 : AnyObject?)在 swift 3.0/Xcode 8 b6 中丢失

ios - 我应该在收到有效载荷后更新我的应用程序吗?或者我应该总是通过允许它自己下载来更新它?

ios - 使用 Windows Azure 在 iOS 中进行 GPS 跟踪的最佳实践?

ios 快速将图像发布到 linkedin