Swift UITableView单元重用标识符
我已经为不同的单元标识符创建了一个结构:Swift UITableView单元重用标识符,swift,uitableview,Swift,Uitableview,我已经为不同的单元标识符创建了一个结构: enum CustomCellIdentifiers { static let cellForCountry = "cellForCountry" static let cellForCity = "cellForCity" static let cellForStoreType = "cellForStoreType" } 我正在根据开关情况将单元格注册到表中,如: view.tableForItems.register(UI
enum CustomCellIdentifiers
{
static let cellForCountry = "cellForCountry"
static let cellForCity = "cellForCity"
static let cellForStoreType = "cellForStoreType"
}
我正在根据开关情况将单元格注册到表中,如:
view.tableForItems.register(UINib.init(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier:cellIdentifier)
但是,当我注册单元格时,我错误地发现reuseIdentifier
为零:
class CustomTableCell: UITableViewCell
{
override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code
switch self.reuseIdentifier ?? "cellForCountry" //It only work with country cell
{
case CustomCellIdentifiers.cellForCountry:
print("cellForCountry") break;
case CustomCellIdentifiers.cellForCity:
print("cellForCity") break;
case CustomCellIdentifiers.cellForStoreType:
print("cellForStoreType")
break;
default: break
}
}
}
在
xib
文件中也设置reUseIdentifier
。
如果无法在xib类中设置reuseIdentifier,则表示您从UIView创建了xib文件。相反
使用UITableviewcell类创建xib而不是UIView。。
然后,您可以从xib文件中分配reuseIdentifier
请看下图
如果要存储简单字符串并希望其不可变,请使用enum而不是Struct。此外,在开关盒中不需要中断。而不是struct,使用枚举存储值。@Jaydeep i已更改,但重用标识符的问题是nilSo。switchCase
switch self中的重用标识符是nilSo。reuseIdentifier
是否没有执行任何一个实例?它如何与optional一起工作?请共享一些详细信息?是否在xib文件中设置了reuseIdentifier?