我正在尝试为我的表格 View 单元格使用自定义附件类型(UIImage),它具有展开/折叠功能。当用户点击单元格时,如果再次点击父单元格,则该行将展开或折叠。
我用来设置配件类型的 ImageView 如下:
var expandIcon : UIImageView?
expandIcon = UIImageView(frame:CGRectMake(0, 0, 16, 16))
expandIcon!.image = UIImage(named:"expand")
下面的代码是当用户点击一行时,如果它的父行应该扩展,或者如果它已经展开,它将折叠。
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 使用率达到 99%(如 xcode 所报告)。如果我删除 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()
关于ios - Swift:对单元使用自定义配件类型会卡住 UI 和 99% 的 CPU 使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931842/