Я работаю над проектом, который показывает несколько строк с тремя разделами в виде таблицы.Если пользователь нажимает на отдельную строку в разделе, этот цвет строки следует отправить в DetailVC.В настоящее время я отправляю один раздел цветов в DetailVC с помощью оператора switch
.Теперь я хочу отправить остальные цвета раздела в DetailVC.
Вот мой код.Я очень старался, но у меня не получилось.Я знаю, что мне нужно изменить код в операторе switch
.
import UIKit
import Foundation
class ViewController: UITableViewController {
let cellID = "Cell"
let twoDimensionalArray = [
["Blue","Green","Red"],
["Orange","Purple"],
["Brown","Yellow"]
]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = 44.0
}
// Mark:- Tableview datasource methods
override func numberOfSections(in tableView: UITableView) -> Int {
return twoDimensionalArray.count
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = "Header"
label.backgroundColor = UIColor.lightGray
return label
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return twoDimensionalArray[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! TableViewCell
cell.imageview.image = UIImage(named : twoDimensionalArray[indexPath.section][indexPath.row])
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard? .instantiateViewController(withIdentifier: "VC2") as! DetailViewController
switch indexPath.row {
case 0:
vc.passedImage = UIImage(named : "Blue")!
self.present(vc, animated: true, completion: nil)
break
case 1:
vc.passedImage = UIImage(named : "Green")!
self.present(vc, animated: true, completion: nil)
break
case 2:
vc.passedImage = UIImage(named : "Red")!
self.present(vc, animated: true, completion: nil)
break
default :
vc.passedImage = UIImage(named : "Blue")!
self.present(vc, animated: true, completion: nil)
}
}
}
Ожидаемый результат заключается в том, что выбранный цвет строки в разделе должен отображаться в DetailVC.Цвета отображаются только в одном разделе, но не во всех разделах таблицы.