2016-10-23 2 views
1

以前にクリックしたブランドに応じてデータを変更できるtableviewを作成しようとしています。たとえば、Acuraブランドをクリックすると、アキュラの車のモデルをクリックすると車の年が表示されます。以下の私のコードの問題は、車のブランドモデルをクリックするとCARBRANDとCARMODELの両方を同時に選択し、CAR YEARを表示することです。それは車の年 enter image description here 両方の車のブランドと車のモデルが既にクリックされたにすべての方法をクリックしておくテーブルビューはちょうど私がUItableviewの可変データソースを作成する(viewcontrollerを使用)

enter image description here

ここから

をクリックした行を選択し続けるように見えますこの時点で。 enter image description here

どのようにしてtableviewが2回クリックされなくなったのですか? クラスjobtypeViewController:のUIViewController、UITableViewDelegate、UITableViewDataSource { するvar thetitle = "アキュラ" "あなたのdidSelectRowAtIndexPath方法で

// 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

、最初if文が実行され、それがtheTitleへの変更します"。そして、次のif文が実行されると、条件(thetitle == "Acura")が真となり、対応する文が実行されます。これを修正するには、ifの代わりにelse 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") 
     } 
    } 
} 

あるいはswitch/caseブロックとして2 if Sを再構築します。

+0

私はコードをコピーして何とかacuraをクリックすると空白に戻ります。 – Alan

+0

@Alanおそらく 'tableviewz.hidden = true'があるからです。 – pbasdf

+0

あなたはあなたの命を救う権利を持っています。どうもありがとうございます – Alan

関連する問題