将信息传递给自定义 TableViewCell - Swift
Passing information to a custom TableViewCell - Swift
问题是这样的:我尝试了很多方法将信息从 SecondViewController 传递给 ViewController1。在第一个 ViewController1 中,我有一个 tableView,在这个 tableView 中,每个单元格都会收到一个图像和一个标签。我想要做的是,在 SecondViewController 中,我将在文本字段中写入一些内容并选择一些随机图像并将其显示在 ViewController1 中。
有人能告诉我怎么做吗?我尝试了委托、dequeuereusablecell,但数据不会显示在单元格中。
提前致谢!
编辑
这是我的代码:
ViewController 1:
导入 UIKit
class首先ViewController: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:
导入 UIKit
协议 sendDataToFVCDelegate {
func myVCDidFinish(文本:字符串)
}
class 第二ViewController: 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)
}
}
}
表格视图单元格:
导入 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
}
}
我做错了什么?现在,即使 "popViewControllerAnimated" 行也返回到第一个视图控制器。
非常感谢您的帮助!!
因此您的自定义 TableViewCell 应该有它自己的 class(例如 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) )
在第一个(确认第二个VC委托协议)中,你有
func myVCDidFinish(controller: SecondVC, text: String, image: UIImage) {
textFrom2VC = text
imageFrom2VC = image
controller.navigationController?.popViewControllerAnimated(true)
self.tableView.reloadData()
}
问题是这样的:我尝试了很多方法将信息从 SecondViewController 传递给 ViewController1。在第一个 ViewController1 中,我有一个 tableView,在这个 tableView 中,每个单元格都会收到一个图像和一个标签。我想要做的是,在 SecondViewController 中,我将在文本字段中写入一些内容并选择一些随机图像并将其显示在 ViewController1 中。
有人能告诉我怎么做吗?我尝试了委托、dequeuereusablecell,但数据不会显示在单元格中。
提前致谢!
编辑
这是我的代码:
ViewController 1:
导入 UIKit
class首先ViewController: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:
导入 UIKit
协议 sendDataToFVCDelegate { func myVCDidFinish(文本:字符串) }
class 第二ViewController: 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)
}
}
}
表格视图单元格:
导入 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
}
}
我做错了什么?现在,即使 "popViewControllerAnimated" 行也返回到第一个视图控制器。
非常感谢您的帮助!!
因此您的自定义 TableViewCell 应该有它自己的 class(例如 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) )
在第一个(确认第二个VC委托协议)中,你有
func myVCDidFinish(controller: SecondVC, text: String, image: UIImage) {
textFrom2VC = text
imageFrom2VC = image
controller.navigationController?.popViewControllerAnimated(true)
self.tableView.reloadData()
}