将信息传递给定制的TableViewCell - Swift [英] Passing information to a custom TableViewCell - Swift

查看:183
本文介绍了将信息传递给定制的TableViewCell - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有人可以告诉我怎么做吗?我尝试过代理,dequeuereusablecell,数据不会显示在单元格中。



提前感谢!



编辑



这是我的代码:



ViewController 1:



import UIKit



class FirstViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,sendDataToFVCDelegate {

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

覆盖func viewDidLoad(){
super.viewDidLoad()
//在加载视图后执行任何其他设置,通常从一个笔尖
}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}


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
返回单元格!
}

}



ViewController 2:



import UIKit



协议sendDataToFVCDelegate {
func myVCDidFinish(text:String)
}



class SecondViewController:UIViewController {

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

覆盖func viewDidLoad(){
super.viewDidLoad()

//加载视图后执行任何其他设置。
}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}

@IBAction func addButton(sender:AnyObject){

如果textField == nil {return}
let name = textField.text
println(\(name))

如果委托== nil {return}
委托?.myVCDidFinish(name)

如果让navigation = self.navigationController {
navigation.popViewControllerAnimated(true)
}
}

}



TableViewCell:



import UIKit



class CustomCellTableViewCell:UITableViewCell {

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



覆盖func awakeFromNib(){
super.awakeFromNib()
//初始化代码
}

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

//配置所选状态的视图

}



我在做什么错误?现在即使是popViewControllerAnimated行也将返回到第一个视图控制器。



非常感谢您的帮助!!

解决方案

所以你的自定义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
返回单元格!
}

您必须从其他视图获得这些代理。在第二个VC中,您有

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

您使用

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

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

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


The problem is this: I tried many ways to pass down information to a ViewController1 from a SecondViewController. In this first ViewController1 I have a tableView, and in this tableView, each cell will receive an image and a label. What I want to do is, in the SecondViewController, I will write something in a textField and pick some random image and display it in the ViewController1.

Could someone tell me how to do it? I tried delegates, dequeuereusablecell and the data won't display in the cell.

Thanks in advance!

EDIT

here's my code:

ViewController 1:

import UIKit

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

}

ViewController 2:

import UIKit

protocol sendDataToFVCDelegate { func myVCDidFinish(text: String) }

class 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 :

import UIKit

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

}

What am I doing wrong? Now even the line "popViewControllerAnimated" is going back to the first view controller.

Thanks very much for your help!!

解决方案

So your custom TableViewCell should have it's own class (for example ImageTableViewCell) which takes care of the image you will set when loading the TableView:

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

and in the 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!
}

And you have to get these from the other view with delegate. In the second VC you have

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

which you call with

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

and in the first (which confirms to protocol SecondVCDelegate) you have

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

这篇关于将信息传递给定制的TableViewCell - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆