【问题标题】:Passing information to a custom TableViewCell - Swift将信息传递给自定义 TableViewCell - Swift
【发布时间】:2015-08-14 07:19:13
【问题描述】:

问题是这样的:我尝试了很多方法将信息从 SecondViewController 传递到 ViewController1。在第一个 ViewController1 中,我有一个 tableView,在这个 tableView 中,每个单元格都会收到一个图像和一个标签。我想做的是,在 SecondViewController 中,我会在 textField 中写一些东西,然后选择一些随机图像并将其显示在 ViewController1 中。

谁能告诉我怎么做?我尝试了代表,dequeuereusablecell,数据不会显示在单元格中。

提前致谢!

编辑

这是我的代码:

视图控制器 1:

导入 UIKit

类 FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, sendDataToFVCDelegate {

@IBOutlet var tableView: UITableView!
var labelsListArray = [""]

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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


func myVCDidFinish(text: String) {
    labelsListArray.append(text)
    tableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return labelsListArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let row = indexPath.row
    let textLabel = labelsListArray[row]
    var cell = tableView.dequeueReusableCellWithIdentifier("ImageView") as? CustomCellTableViewCell
    cell!.cellLabel!.text = textLabel
    return cell!
}

}

视图控制器 2:

导入 UIKit

协议 sendDataToFVCDelegate { func myVCDidFinish(文本:字符串) }

类 SecondViewController: UIViewController {

var delegate:sendDataToFVCDelegate?
@IBOutlet var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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

@IBAction func addButton(sender: AnyObject) {

    if textField == nil { return }
    let name = textField.text
    println("\(name)")

    if delegate == nil { return }
    delegate?.myVCDidFinish(name)

    if let navigation = self.navigationController {
        navigation.popViewControllerAnimated(true)
    }
}

}

TableViewCell:

导入 UIKit

类 CustomCellTableViewCell: UITableViewCell {

@IBOutlet var cellImage: UIImageView!
@IBOutlet var cellLabel: UILabel!



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

我做错了什么?现在连“popViewControllerAnimated”这行代码都回到了第一个视图控制器。

非常感谢您的帮助!!

【问题讨论】:

  • 使用委托模式是正确的。发布您的代码,以便我们看看出了什么问题。
  • 我删除了整个代码。可以发布任何简单的例子来说明如何做到这一点?也许我错过了一个细节或任何东西。我会很高兴的
  • 我有一个简单的项目,它的功能与您需要的类似:github.com/maurovz/MarvelAPIExample

标签: uitableview swift delegates datasource tableviewcell


【解决方案1】:

所以你的自定义 TableViewCell 应该有它自己的类(例如ImageTableViewCell),它负责你在加载 TableView 时设置的图像:

@IBOutlet var titleLabel: UILabel!
@IBOutlet var imageView: UIImageView!

在 TableView 中:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("ImageView") as? ImageTableViewCell
        cell!.titleLabel!.text = textFrom2VC
        cell!.imageView.image = imageFrom2VC
        return cell!
}

而且您必须使用委托从另一个视图中获取这些信息。在第二个 VC 你有

protocol SecondVCDelegate{
    func myVCDidFinish(controller:SecondVC, text: String, image: UIImage)
}

你用

调用
    var delegate:SecondVCDelegate.myVCDidFinish(self, text: "this is random text", image: (your image) )

在第一个(确认协议 SecondVCDelegate)中,您有

func myVCDidFinish(controller: SecondVC, text: String, image: UIImage) {
    textFrom2VC = text
    imageFrom2VC = image
    controller.navigationController?.popViewControllerAnimated(true)
    self.tableView.reloadData()
}

【讨论】:

  • 您将委托设置为可选,这使其为零,然后您仅在它为 not nil 时才设置它,这没有任何意义。因此,如果您删除 if delegate == nil { return } 我认为它应该可以工作...
  • 你是对的。 addButton 现在可以工作了,我写的内容打印在日志中,但字符串没有转到标签,它仍然是空的。知道为什么吗?
猜你喜欢
  • 2011-02-05
  • 2023-02-17
  • 2023-03-21
  • 2017-09-14
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多