2016-10-23 23 views
1

我試圖創建tableview,可以改變其中的數據,取決於之前點擊的品牌,例如,如果我點擊謳歌品牌tableview然後將數組改成謳歌陣列,當點擊Acura中的車型時,它將顯示車年。我的代碼下面的問題是,當點擊汽車品牌模型時,它同時選擇CARBRAND和CARMODEL,並顯示CAR YEAR。它似乎在tableview中只是不斷的選擇,我從這裏爲UItableview創建可變數據源(使用視圖控制器)

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") 
       } 
      } 
} 

回答

1

在你didSelectRowAtIndexPath方法,在執行第一if聲明,它改變theTitle爲 「謳歌」。然後,當執行下一個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 S作爲switch/case塊。

+0

我複製了代碼,並在點擊acura後返回空白。 – Alan

+0

@Alan可能因爲你有'tableviewz.hidden = true'。 – pbasdf

+0

你是對的,哈哈,你是一個拯救生命的人。非常感謝 – Alan

相關問題