I am working on a project which shows multiple rows with three sections in a tableview. If the user taps on the individual row in a section, that row color should be sent to DetailVC. Currently I am sending one section of colors to DetailVC by using a switch
statement. Now I want to send the remaining section colors to DetailVC.
Here is my code. I pretty much tried, but it didn't work for me. I know that I have to change code in the switch
statement.
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)
}
}
}
The expected result is that the selected row color in a section is to be shown in DetailVC. It is showing the colors only in the one section, but not in all the sections in a tableview.
Since you have a multidimensional array you can access it using the indexPath
section
and row
properties to get the color String
for that indexPath
. Then you can create your UIImage
and set it on your detail view controller.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard? .instantiateViewController(withIdentifier: "VC2") as! DetailViewController
let color = twoDimensionalArray[indexPath.section][indexPath.row]
vc.passedImage = UIImage(named : color)!
self.present(vc, animated: true, completion: nil)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.