簡體   English   中英

快速鑄造錯誤應該在哪里

[英]Swift casting error where there shouldn't be

我試圖顯示兩個xib文件之一,具體取決於驅動UITable的數組內部,但是在以下函數中出現轉換錯誤:

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

由於以下錯誤,此功能失敗:

無法將類型為'Phlare.ContactCell'(0x1056a1fe8)的值強制轉換為'Phlare.DropDownOptionsCell'

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

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

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

希望能幫助到你。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM