问题
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!!
回答1:
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()
}
来源:https://stackoverflow.com/questions/30563205/passing-information-to-a-custom-tableviewcell-swift