我有一个UITableView,它将数据(图像和标签)从单元格传递到DetailViewController。这对我所有的20个单元格都适用!
但是现在我尝试在DetailViewController中创建一个数组,该数组为我提供“ Comm1Description”和“ Comm1Description”(只是“ arr2”中的简单文本)。 Cell1的Comm1Description,Cell2的Comm2Description等。
这是我的代码
import UIKit
class WCommanderController: UIViewController {
@IBOutlet weak var tbl: UITableView!
var arr = ["Commander1", "Commander2"]
var arr2 = ["Comm1Description", "Comm2Description"]
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.
}
}
extension WCommanderController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 130
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? DataTableViewCell
cell?.img.image = UIImage(named: arr[indexPath.row])
cell?.lbl.text = arr[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController
vc?.str = arr[indexPath.row]
navigationController?.pushViewController(vc!, animated: true)
}
}
答案 0 :(得分:0)
如果您的表格单元格中有一个textview,那么可以做到这一点。
let dictionary1 = ["KeyCommander": "Commander1", "KeyDescription": "Comm1Description1", "KeyTextField": "TextField1"]
let dictionary2 = ["KeyCommander": "Commander2", "KeyDescription": "Comm1Description2", "KeyTextField": "TextField2"]
现在,将数组设为变量。
var array = [dictionary1, dictionary2]
在CellForRowAtIndex上,给文本字段添加一个标签。
array[textfield.tag]["KeyTextField"] = "Typed Value"