我有一个分组的tableview,它包含两行,每行都有一个自定义的tableview单元格。我已经建立了自定义tableview类和单元标识符,并与接口构建器中的tableview行相关联。我的第一个tableview单元看起来很好,但第二个看起来与第一个单元格具有相同的属性。但是,一旦我点击第二个单元,然后点击第一个单元,第二个单元切换到正确的单元设计。
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("EditCellID", forIndexPath: indexPath) as! EditCell
if indexPath.row == 0
{
//Do some stuff
return cell
}
if indexPath.row == 1
{
let cell = tableView.dequeueReusableCellWithIdentifier("DeleteCellID", forIndexPath: indexPath) as! DeleteCell
//Do some stuff
return cell
}
return cell
}
答案 0 :(得分:0)
试试这个:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell!
if indexPath.row == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("EditCellID", forIndexPath: indexPath) as! EditCell
// Now you can access the subclass attributes by doing : (cell as! EditCell).theAttribute
} else {
cell = tableView.dequeueReusableCellWithIdentifier("DeleteCellID", forIndexPath: indexPath) as! DeleteCell
}
return cell
}
在作用域中只创建一个单元格对象,并根据行将对象初始化为特定UITableViewCell子类的实例。
更新:添加了关于如何访问UITableViewCell子类属性的注释。