[英]Check if custom TableViewCell is nil
I have two prototype cells BasicCell
and BasicCell2
. 我有两个原型单元
BasicCell
和BasicCell2
。 Both have a label titlet
in it. 两者都有标签
titlet
。 I am unable to check for nil cel 我无法检查零元
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row == 0 && flagcheck == 0 ) {
let cell2 = tableView.dequeueReusableCellWithIdentifier("BasicCell2") as BasicCell2
if cell2 == nil {
cell2 = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BasicCell2")
}
cell2.titlet.text = question+"\n"+"\n"
cell2.backgroundColor = hexStringToUIColor("b2cecf")
cell2.userInteractionEnabled = false
return cell2
}
else if (indexPath.row == 0){
let cell = tableView.dequeueReusableCellWithIdentifier("BasicCell") as BasicCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BasicCell")
}
cell.titlet.text = question+"\n"+"\n"
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier("BasicCell") as BasicCell
if cell === nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BasicCell")
}
cell.titlet.text = self.data1[indexPath.row-1]
return cell
}
}
This is the error which I get 这是我得到的错误
Cannot invoke '==' with an argument list of type '(BasicCell, NilLiteralConvertible)'
This is my BasicCell.swift 这是我的BasicCell.swift
import UIKit
class BasicCell: UITableViewCell {
@IBOutlet weak var titlet: UILabel!
}
You might have to unwrap the cell before performing the comparison operation. 您可能必须先展开单元格,然后才能执行比较操作。
let cell2 = tableView.dequeueReusableCellWithIdentifier("BasicCell2") as BasicCell2!
if cell2 == nil {
cell2 = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BasicCell2")
}
Check the BasicCell! 检查BasicCell! (The "!" character is unwrapping character).
(“!”字符是展开字符)。
Let me know if this works. 让我知道这个是否奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.