我試圖創建tableview,可以改變其中的數據,取決於之前點擊的品牌,例如,如果我點擊謳歌品牌tableview然後將數組改成謳歌陣列,當點擊Acura中的車型時,它將顯示車年。我的代碼下面的問題是,當點擊汽車品牌模型時,它同時選擇CARBRAND和CARMODEL,並顯示CAR YEAR。它似乎在tableview中只是不斷的選擇,我從這裏爲UItableview創建可變數據源(使用視圖控制器)
點擊
我該如何停止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")
}
}
}
我複製了代碼,並在點擊acura後返回空白。 – Alan
@Alan可能因爲你有'tableviewz.hidden = true'。 – pbasdf
你是對的,哈哈,你是一個拯救生命的人。非常感謝 – Alan