我正在尝试为具有展开/折叠功能的表视图单元格使用自定义附件类型(UIImage)。当用户点击单元格时,如果再次点击父单元格,则行将展开或折叠。
我用来设置配件类型的图像视图如下:
var expandIcon : UIImageView?
expandIcon = UIImageView(frame:CGRectMake(0, 0, 16, 16))
expandIcon!.image = UIImage(named:"expand")
以下代码是当用户点击一行时,如果它的父行,它应该 epxand,或者如果它已经展开,它将折叠。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellData = dataForCellAtRowIndex[indexPath.section]!.rows[indexPath.row]
var cell:UITableViewCell!
if isParentCell(indexPath.row) == true {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = "test" + String(indexPath.row)
cell.detailTextLabel?.text = "detail"
cell.backgroundColor = UIColor.whiteColor()
cell.accessoryView = expandIcon
}else{
cell = tableView.dequeueReusableCellWithIdentifier("childCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.lightGrayColor()
cell.textLabel?.text = "child name"
cell.detailTextLabel?.text = "child detail"
cell.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}
导致问题的位是cell.accessoryView = expandAccessory
,这会导致 UI 冻结,并且 CPU 使用率达到 xcode 报告的 99%。 如果我删除cell.accessoryView = expandIcon
一切都很好!为什么会这样?
你应该实现一个函数来返回一个 expandIcon,然后调用它来代替 expandIcon 变量。
func expandImageView() -> UIImageView {
let expandIcon = UIImageView(frame:CGRectMake(0, 0, 16, 16))
expandIcon.image = UIImage(named:"expand")
return expandIcon
}
cell.accessoryView = expandImageView()
@rmaddy提到的修复是重用导致UI冻结的UIImage视图。为每个单元格创建一个新的 UIImage 解决了我的问题,而不是:
if isParentCell(indexPath.row) == true {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = "test" + String(indexPath.row)
cell.detailTextLabel?.text = "detail"
cell.backgroundColor = UIColor.whiteColor()
cell.accessoryView = expandIcon
}
我不得不:
if isParentCell(indexPath.row) == true {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = "test" + String(indexPath.row)
cell.detailTextLabel?.text = "detail"
cell.backgroundColor = UIColor.whiteColor()
//put the below two lines in a func and return a UIImage
let expandIcon = UIImageView(frame:CGRectMake(0, 0, 16, 16))
expandIcon.image = UIImage(named:"expand")
cell.accessoryView = expandIcon
}