[英]UITableView with two custom cells not appearing properly
我有一个由两行组成的分组表视图,每行都有一个自定义表视图单元格。 我有自定义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
}
尝试这个:
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子类属性的注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.