【问题标题】:Swift : How do I pass the same text and image form tableVC to a detailVC using the same tableviewcell?Swift:如何使用相同的 tableviewcell 将相同的文本和图像表单 tableVC 传递给 detailVC?
【发布时间】:2015-05-17 08:34:15
【问题描述】:

我正在尝试使用 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] 中有一个错误

detailVC

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
}

故事板 http://s18.postimg.org/mwkw5hf1l/image.png

【问题讨论】:

    标签: ios xcode swift tableview tableviewcell


    【解决方案1】:

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

    【讨论】:

    • 修复了第一个谢谢兄弟,如果你能帮我把图片和标题传递给detailVC
    【解决方案2】:

    我觉得这个有问题

    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
    
        }
    }
    

    var text = self.tableView.indexPathForSelectedRow() 不是文本,它是 indexPath

    在这个函数的范围内,thecourseName 不可能是你期望的那样。我认为你想要的是更多类似这些方面的东西。

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if ( segue == "toInfo") {
            var text = thecourseName[self.tableView.indexPathForSelectedRow()]
            var detailsVC: DetalisTableViewController = segue.destinationViewController as! DetalisTableViewController
            detailsVC.courseName = text
    
        }
    }
    

    这应该为您提供所选单元格的文本并将其设置为详细信息视图的variable

    【讨论】:

      猜你喜欢
      • 2022-11-04
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多