gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:31:27 25 4
gpt4 key购买 nike

我正在尝试创建 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com