我想传递TableView中包含的存储数据,然后将其传递给另一个ViewController。我的Customcell有一个UIIMAGE和一个字符串。当用户按单元格时,我想用uiimage显示一个"详细视图控制器",并显示包含所选单元格信息的标签。
这是我的代码:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var dataTableView: UITableView!
var myList = [dataList]()
var textToBeSent: String = ""
var selectedImage: UIImage?
var selectedLabel: String?
//Load Items To List
func loaditems(){
let item1 = dataList(photoList: UIImage.self(), itemDescription: "Descripcion Aqui")
let item2 = dataList(photoList: UIImage.self(), itemDescription: "Aqui tmb")
myList += [item1,item2]
}
//var list = ["Documento 1", "Documento 2", "Documento 3"]
override func viewDidLoad() {
super.viewDidLoad()
if let savedData = loadSavedItems(){
myList += savedData
} else {
loaditems()
}
//dataTableView.register(UITableViewCell.self, forCellReuseIdentifier: "reusablecell")
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return myList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "prototype", for: indexPath) as! PrototypeCell
let itemsinCell = myList[indexPath.row]
cell.imageItem.image = itemsinCell.photoList
cell.itemDescription.text = String(itemsinCell.itemDescription)
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
myList.remove(at: indexPath.row)
dataTableView.reloadData()
}
saveToSavedData()
}
这是我要传递某个单元格的数据的功能。数据来自使用AdeCoder NScoder存储在"数据库"中的Swift文件。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")
selectedImage! = myList[indexPath.row].photoList
selectedLabel! = myList[indexPath.row].itemdescription
performSegue(withIdentifier: "selectedRowSegue", sender: myList[indexPath.row])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "selectedRowSegue"){
let chosenRowViewController = segue.destination as! chosenRowViewController
chosenRowViewController.image3 = selectedImage?.photoList
chosenRowViewController.label3 = selectedLabel?.itemDescription
}
}
放开一个segue,以便用以前的数据填充细胞ViewController:
//Unwinde Segue
@IBAction func unWindlToList(sender: UIStoryboardSegue){
if let sourceViewController = sender.source as? ProcessViewController, let item = sourceViewController.item{
let newIndexPath = IndexPath(row: myList.count, section: 0)
myList.append(item)
dataTableView.insertRows(at: [newIndexPath], with: .automatic)
}
saveToSavedData()
}
//Archive Data
func saveToSavedData(){
NSKeyedArchiver.archiveRootObject(myList, toFile: (dataList.fileFolder?.path)!)
}
//Unarchive Data
func loadSavedItems() -> [dataList]?{
return NSKeyedUnarchiver.unarchiveObject(withFile: (dataList.fileFolder?.path)!) as? [dataList]
}
}
class PrototypeCell: UITableViewCell {
@IBOutlet weak var itemDescription: UILabel!
@IBOutlet weak var imageItem: UIImageView!
}
只需将prepare(for segue: UIStoryboardSegue, sender: Any?)
函数替换为以下代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "selectedRowSegue"), let list = sender as? dataList {
let chosenRowViewController = segue.destination as! chosenRowViewController
chosenRowViewController.image3 = list.photoList
chosenRowViewController.label3 = list.itemDescription
}
}
有几种突出的事情。
1- var myList = [dataList]()
数据师是Class
,应该大写。它应该是var myList = [DataList]()
2-您将其作为类属性,但是它在您发布的代码中没有使用它,所以您为什么添加它?它的目的是什么?var textToBeSent: String = ""
3-您有这两个类属性变量
var selectedImage: UIImage?
var selectedLabel: String?
保存来自[myList]
的数据,但您确实不需要它们,因为您可以在prepareForSegue
中使用dot notation
从[myList]
访问数据(在Preparforsegue中阅读评论的代码(。
4-在Preparforsegue中,您有let chosenRowViewController = segue.destination as! chosenRowViewController
。ChosenRowViewController是一类,应该像这样大写:
let chosenRowViewController = segue.destination as! ChosenRowViewController // it's capitalized after the as!
这是清理的代码。
@IBOutlet weak var dataTableView: UITableView!
var myList = [DataList]()
func loaditems(){
let item1 = dataList(photoList: UIImage.self(), itemDescription: "Descripcion Aqui")
let item2 = dataList(photoList: UIImage.self(), itemDescription: "Aqui tmb")
myList.append(item1)
myList.append(item2)
}
override func viewDidLoad() {
super.viewDidLoad()
// everything you already have inside here...
}
// your tableView datasource methods...
5-由于您使用prepareForSegue
,您不需要didSelectRowAt indexPath
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "selectedRowSegue"){
// get the indexPath for the selectedRow
let indexPath = tableView.indexPathForSelectedRow
let chosenRowViewController = segue.destination as! ChosenRowViewController
chosenRowViewController.image3 = myList[indexPath!.row].photoList // use dot notation to access the photoList property
chosenRowViewController.label3 = myList[indexPath!.row].itemDescription // use dot notation to access the itemDescription property
}
}
您在didSelectRowAt
中犯了一个错误,在调用performSegue
时,您必须通过sender
的控制器,如果您的当前UIVIewController
,则必须将self
作为当前发件人控制器。因此,您所校正后的Methog看起来像在代码片段下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")
selectedImage! = myList[indexPath.row].photoList
selectedLabel! = myList[indexPath.row]
performSegue(withIdentifier: "selectedRowSegue", sender: self)
}