问题
I have 3 view controllers: FirstVC for creating 9 items, SecondVC is a tableView to sort those items by category name/id and display both, and ThirdVC is a tableView to extract and show the items inside that category. I'm trying to implement a tableView drill-down to do that but the secondVC and thirdVC has me stumped.
This is the outcome I'm looking for:
FirstVC
-9 objects :
3 Red objects
Cherry, Tomato, Ruby
3 Green objects
Cucumber, Broccoli, Emerald
3 Blue objects
Blueberries, Sky, Sapphire
1.create all 9 objects
2.the object color and/or id determine which category each object belongs in the secondVC
//There is no need to display anything on this scene
SecondVC
-3 cells:
-Red
-Green
-Blue
//this is where I'm stumped
1.tableView that sorts all 9 objects based on their color and/or id
2.right now all 9 cells are displaying but I would only like 3 cells to show only each individual category name and id
ThirdVC
-3 more cells
Cherry
Tomato
Ruby
1.If you pressed the Red category cell in the secondVC then these 3 cells above would show inside the thirdVC. Same concept for the Blue and Green cells
Data model object
class ColorClass: NSObject{
var colorCategoryName: String?
var colorID: String?
var realWorldObject: String?
}
My first view controller instantiates 9 different objects from this data model
import UIKit
class FirstViewController: UIViewController {
var firstColorArray = [ColorClass]()
override func viewDidLoad() {
super.viewDidLoad()
let redColor0 = ColorClass()
redColor0.colorCategoryName = "red"
redColor0.colorID = "0000"
redColor0.realWorldObject = "Cherry"
self.firstColorArray.append(redColor0)
let redColor1 = ColorClass()
redColor1.colorCategoryName = "red"
redColor1.colorID = "0000"
redColor1.realWorldObject = "Tomato"
self.firstColorArray.append(redColor1)
let redColor2 = ColorClass()
redColor2.colorCategoryName = "red"
redColor2.colorID = "0000"
redColor2.realWorldObject = "Ruby"
self.firstColorArray.append(redColor2)
let greenColor0 = ColorClass()
greenColor0.colorCategoryName = "green"
greenColor0.colorID = "1000"
greenColor0.realWorldObject = "Cucumber"
self.firstColorArray.append(greenColor0)
let greenColor1 = ColorClass()
greenColor1.colorCategoryName = "green"
greenColor1.colorID = "1000"
greenColor1.realWorldObject = "Broccoli"
self.firstColorArray.append(greenColor1)
let greenColor2 = ColorClass()
greenColor2.colorCategoryName = "green"
greenColor2.colorID = "1000"
greenColor2.realWorldObject = "Emerald"
self.firstColorArray.append(greenColor2)
let blueColor0 = ColorClass()
blueColor0.colorCategoryName = "blue"
blueColor0.colorID = "2000"
blueColor0.realWorldObject = "Blueberries"
self.firstColorArray.append(blueColor0)
let blueColor1 = ColorClass()
blueColor1.colorCategoryName = "blue"
blueColor1.colorID = "2000"
blueColor1.realWorldObject = "Sky"
self.firstColorArray.append(blueColor1)
let blueColor2 = ColorClass()
blueColor2.colorCategoryName = "blue"
blueColor2.colorID = "2000"
blueColor2.realWorldObject = "Sapphire"
self.firstColorArray.append(blueColor2)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueToSecondVC"{
let secondVC = segue.destinationViewController as! SecondViewController
secondVC.colorCategoryNameArray = self.firstColorArray
}
}
}
My SecondVC to categorize the objects
import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var colorCategoryNameArray = [ColorClass]()
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.colorCategoryNameArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("colorCategoryNameCell", forIndexPath: indexPath)
let rowIndex = self.colorCategoryNameArray[indexPath.row]
cell.textLabel?.text = rowIndex.colorCategoryName
cell.detailTextLabel?.text = rowIndex.colorID
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let cell = sender as! UITableViewCell
let selectedRow = tableView.indexPathForCell(cell)?.row
let thirdTableVC = segue.destinationViewController as! ThirdViewController
thirdTableVC.realWorldObjectArray = [colorCategoryNameArray[selectedRow!]]
}
}
My ThirdVC to display the 3 objects inside each category
import UIKit
class ThirdViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var realWorldObjectArray = [ColorClass]()
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.realWorldObjectArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("realWordObjectCell", forIndexPath: indexPath)
let rowIndex = self.realWorldObjectArray[indexPath.row]
cell.textLabel?.text = rowIndex.realWorldObject
return cell
}
}
来源:https://stackoverflow.com/questions/39663089/uitableview-category-array-drill-down-swift-ios