UITableView类别数组深入研究-Swift iOS

时间:2016-09-23 14:18:54

标签: ios arrays swift tableview drilldown

我有3个视图控制器:FirstVC用于创建9个项目,SecondVC是一个tableView,用于按类别名称/ id对这些项目进行排序并显示两者,而ThirdVC是一个tableView,用于提取和显示该类别中的项目。我正在尝试实现tableView下钻来做到这一点,但是第二个VC和第三个VC让我感到难过。

这是我正在寻找的结果:

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

数据模型对象

class ColorClass: NSObject{
    var colorCategoryName: String?
    var colorID: String?
    var realWorldObject: String?
}

我的第一个视图控制器从此数据模型中实例化了9个不同的对象

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
        }
    }
}

我的SecondVC对象进行分类

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!]]


    }
}

我的ThirdVC显示每个类别中的3个对象

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
    }
}

0 个答案:

没有答案