2017-09-26 38 views
0

我試圖顯示這取決於是什麼陣列驅動UITable人口內部的兩個XIB文件中的一個,但是我在下面的函數得到一個鑄造錯誤:斯威夫特鑄造錯誤的,不應該有

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if(phoneNo[indexPath.row] == "dropdowncell") { 
      //this means that the cell populated should be a dropdown cell 
      let nib2 = UINib(nibName: "DropDownOptionsCell", bundle: nil) 
      print("dropdown if statement hit son") 

      self.myTableView.register(nib2, forCellReuseIdentifier: "cell") 
      let cell2 = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DropDownOptionsCell 

      //cell2.dropDown.tag = indexPath.row 
      //cell2.dropDown.addTarget(self, action: #selector(self.test(sender:)), for:UIControlEvents.touchUpInside) 

      return cell2 
     } 
     else { 
      //this means the cell populated should be a contact cell 
      let nib = UINib(nibName: "ContactCell", bundle: nil) 

      self.myTableView.register(nib, forCellReuseIdentifier: "cell") 
      let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ContactCell 

      print("/n/n below is the cell") 
      print(cell) 

      //below gets the index 
      var val = indexPath.row 

      print("beliw is the name") 
      print(name) 

      print("/n/n below is the val") 
      print(val) 

      cell.dropDown.tag = indexPath.row 
      cell.dropDown.addTarget(self, action: #selector(self.test(sender:)), for:UIControlEvents.touchUpInside) 

      if(phoneNo.contains("dropdowncell")) { 
       //this gets rid of uneven amount in the two arrays 
       var testArray = [String]() 
       testArray = phoneNo 
       print(phoneNo) 
       print("drop down index below") 
       print(dropDownIndex) 
       let testInd = testArray.remove(at: dropDownIndex) 
       print("below is the testArray") 
       print(testArray) 
       val = testArray.count 
      } 

      //below assigns the values of the fields on the cell 
      //cell.contactName.text = (name[val]) 

      return cell 
     } 

     return UITableViewCell() 
    } 

此功能下面的錯誤失敗原因:

Could not cast value of type 'Phlare.ContactCell' (0x1056a1fe8) to 'Phlare.DropDownOptionsCell'

此錯誤是在下面的代碼行中發生:

let cell2 = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DropDownOptionsCell 
+1

不要爲兩個單元使用相同的標識符。另外,爲什麼'register()'cellForRowAt()'裏面? – Larme

+0

'Phlare.ContactCell'是Phlare.DropDownOptionsCell的一個子類嗎? –

+0

@mag_zbc不,它們都是xib的swift文件,我試圖在tableview中加載 – Drew

回答

0

兩個單元不能有相同的單元標識符。運行前請仔細匹配任一單元格上的標識符。

希望它有幫助。

我剛剛通過更換正確的單元格標識符更正了您的代碼。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if(phoneNo[indexPath.row] == "dropdowncell") { 
      //this means that the cell populated should be a dropdown cell 
      let nib2 = UINib(nibName: "DropDownOptionsCell", bundle: nil) 
      print("dropdown if statement hit son") 

      self.myTableView.register(nib2, forCellReuseIdentifier: "cell") 
      let cell2 = myTableView.dequeueReusableCell(withIdentifier: "DropDownOptionsCell", for: indexPath) as! DropDownOptionsCell 

      //cell2.dropDown.tag = indexPath.row 
      //cell2.dropDown.addTarget(self, action: #selector(self.test(sender:)), for:UIControlEvents.touchUpInside) 

      return cell2 
     } 
     else { 
      //this means the cell populated should be a contact cell 
      let nib = UINib(nibName: "ContactCell", bundle: nil) 

      self.myTableView.register(nib, forCellReuseIdentifier: "cell") 
      let cell = myTableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! ContactCell 

      print("/n/n below is the cell") 
      print(cell) 

      //below gets the index 
      var val = indexPath.row 

      print("beliw is the name") 
      print(name) 

      print("/n/n below is the val") 
      print(val) 

      cell.dropDown.tag = indexPath.row 
      cell.dropDown.addTarget(self, action: #selector(self.test(sender:)), for:UIControlEvents.touchUpInside) 

      if(phoneNo.contains("dropdowncell")) { 
       //this gets rid of uneven amount in the two arrays 
       var testArray = [String]() 
       testArray = phoneNo 
       print(phoneNo) 
       print("drop down index below") 
       print(dropDownIndex) 
       let testInd = testArray.remove(at: dropDownIndex) 
       print("below is the testArray") 
       print(testArray) 
       val = testArray.count 
      } 

      //below assigns the values of the fields on the cell 
      //cell.contactName.text = (name[val]) 

      return cell 
     } 

     return UITableViewCell() 
    }