swift - 为 UItableview 创建可变数据源(使用 viewcontroller)

标签 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 单击两次。 类 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")
                }
            }
}

最佳答案

在您的 didSelectRowAtIndexPath 方法中,当执行第一个 if 语句时,它会将 theTitle 更改为“Acura”。那么在执行下一个if语句时,其条件(thetitle == "Acura")为真,则执行相应的语句。要修复它,请对第二个 if 语句使用 else if 而不是 if :那么只有在第一个 if 条件为假。

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 block 。

关于swift - 为 UItableview 创建可变数据源(使用 viewcontroller),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40205827/

相关文章:

ios - 如何在 `UIImageView` 中的自定义单元格中更改 `UITableView`

ios - 在 UITableView 的单元格中选择特定的字符串值

string - 无法将类型 'Range<Int>' 的值转换为预期的参数类型 'Range<Index>'(又名 'Range<String.CharacterView.Index>')

swift - 使用 swift 从另一个函数访问闭包内的变量

ios - TableView 取数据时向下移动

ios - 如何在特定的 indexpath.row 上显示自定义 tableviewcell

ios - 重复淡入淡出效果不起作用

swift - swift 中 DispatchQueue 类型之间的区别

ios - 删除包含标题等于 tableView textLabel 的注释

ios - 在 swift 2.0 中一次显示一个字符的文本