我试图显示两个xib文件中的一个,具体取决于驱动UITable Population的数组内部的内容,但是我在以下函数中遇到了一个转换错误:
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
答案 0 :(得分: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()
}