为UItableview创建可更改的数据源(使用viewcontroller)

时间:2016-10-23 17:33:07

标签: swift uitableview

我正在尝试创建可以根据之前点击的品牌更改其中的数据的tableview,例如,如果我点击Acura品牌,tableview将把阵列更改为Acura数组,当Acura中的汽车模型是点击它将显示汽车年份。下面我的代码的问题是,当点击汽车品牌型号时,它同时选择CARBRAND和CARMODEL并显示CAR YEAR。似乎tableview只是继续选择我点击的行

从这里

enter image description here

然后它一直点击到汽车年份 enter image description here 汽车品牌和汽车模型已经点击了这一点。 enter image description here

如何阻止tableview点击两次。     class jobtypeViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {         var thetitle =“”

    // car make
        var carbrand = ["Acura","Aston_Martin","AUDI","Bentley","BMW","Buick","Cadillac","Chevrolet","Dodge","FIAT","Ford","Genesis","GMC","Honda","Hyundai","Infinit","Jaguar","JEEP","KIA","Landrover","Lexus","Lincoln","Mazda","MercedezBenz","MINI","Mitsubishi","Nissan","Porsche","Scion","Subaru","Suzuki","Toyota","Volkswagen","Volvo"]

        // car model
        var Acura = ["ILX","MDX","NSX","RDX","RLX","RLX Sport Hybrid","TLX"]
        // car year
        var caryear = ["1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017"]

       func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            switch thetitle {
            case "Acura":
                return Acura.count
            case "brand":
                return carbrand.count
            case "model":
                return Honda.count
            case "year":
                return caryear.count
            default:
                return 0
            }
        }
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            switch thetitle {
            case "Acura":
                cell.textLabel?.text = Acura[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            case "brand":
                cell.textLabel?.text = carbrand[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            case "model":
                cell.textLabel?.text = Honda[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            case "year":
                cell.textLabel?.text = caryear[indexPath.row]
                cell.textLabel?.textAlignment = .Center

            default:
                cell.textLabel?.text = ""
            }

            return cell
        }


        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            if thetitle == "automotive"{
                switch carbrand[indexPath.row]{
                case "Acura":
                    print(carbrand[indexPath.row])
                    tableviewz.hidden = true
                    thetitle = "Acura"
                    tableviewz.deselectRowAtIndexPath(tableviewz.indexPathForSelectedRow!, animated: true)
                    tableviewz.reloadData()
                default:
                    print("hello")
                }
            }

            if thetitle == "Acura"{
                switch Acura[indexPath.row]{
                case "ILX":
                    print(Acura[indexPath.row])
                    tableviewz.hidden = true
                    thetitle = "year"
                    tableviewz.reloadData()
                    tableviewz.hidden = false
                default:
                    print("hello")
                }
            }
}

1 个答案:

答案 0 :(得分:1)

didSelectRowAtIndexPath方法中,当执行第一个if语句时,它会将theTitle更改为“Acura”。然后,当执行下一个if语句时,其条件(thetitle == "Acura")为真,并执行相应的语句。要解决此问题,请使用else if代替if作为第二个if语句:只有在第一个if条件为false时才会执行该语句。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if thetitle == "automotive"{
        switch carbrand[indexPath.row]{
            case "Acura":
                print(carbrand[indexPath.row])
                tableviewz.hidden = true
                thetitle = "Acura"
                tableviewz.deselectRowAtIndexPath(tableviewz.indexPathForSelectedRow!, animated: true)
                tableviewz.reloadData()
            default:
                print("hello")
        }
    } else if thetitle == "Acura" {
        switch Acura[indexPath.row]{
            case "ILX":
                print(Acura[indexPath.row])
                tableviewz.hidden = true
                thetitle = "year"
                tableviewz.reloadData()
                tableviewz.hidden = false
            default:
                print("hello")
        }
    }
}

或者将两个if重组为switch/case块。