[英]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.