ios - swift : How do I pass the same text and image form tableVC to a detailVC using the same tableviewcell?

标签 ios xcode swift tableview tableviewcell

我正在尝试使用在 tableVC 中填充的数组将相同的(文本 + 图像)从 tableVC 传递到详细信息 tableVC。

它在 tableVC 中工作,但没有数据传递到 detailVC。

他们共享一个 tableviewcell。

类 TableViewCell:UITableViewCell {

@IBOutlet var img: UIImageView!
@IBOutlet var title: UILabel!
}

表VC

class TableViewController: UITableViewController {

var thecourseName = [String]()
var theimg = [UIImage]()


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]
    cell.date.text = date[indexPath.row]
    cell.img.image = UIImage[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

    }

}

`cell.img.image = UIImage[indexPath.row] 中的一个错误

细节VC

import UIKit

类 DetalisTableViewController: UITableViewController {

   var courseName = [String]()

@IBOutlet var passedCourse: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()


    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return courseName.count
}


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

最佳答案

要修复第一个错误,请将 cell.img.image = UIImage[indexPath.row] 更改为 cell.img.image = theimg[indexPath.row]

关于ios - swift : How do I pass the same text and image form tableVC to a detailVC using the same tableviewcell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056323/

相关文章:

ios - 使用ForEach删除动画的SwiftUI自定义列表不起作用

ios - 如何使用自动布局调整 UILabel 宽度以适合文本?

ios - 检查 UIColor 是暗的还是亮的?

objective-c - 为 iOS 创建静态链接库

ios - 内容 View 自动布局

ios - 如何给 MKAnnotationView 添加回调?

swift - SceneKit Cocoa快照声明失败

swift - XMPPFramework Swift 获取对象始终为空

ios - ScrollView 滚动时页面控制不改变

ios - MPMoviePlayerController 正在播放 YouTube 视频吗?