我正在尝试创建可以根据之前点击的品牌更改其中的数据的tableview,例如,如果我点击Acura品牌,tableview将把阵列更改为Acura数组,当Acura中的汽车模型是点击它将显示汽车年份。下面我的代码的问题是,当点击汽车品牌型号时,它同时选择CARBRAND和CARMODEL并显示CAR YEAR。似乎tableview只是继续选择我点击的行
从这里
然后它一直点击到汽车年份 汽车品牌和汽车模型已经点击了这一点。
如何阻止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")
}
}
}
答案 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
块。