ios - Swift:对单元使用自定义配件类型会卡住 UI 和 99% 的 CPU 使用率

标签 ios xcode swift uitableview

我正在尝试为我的表格 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/

相关文章:

iphone - 将 UIButton 修复在 UIScrollView 之上

ios - Spritekit - 重叠文本

iphone - 在现有屏幕上显示小 View /对话框的最佳方式?

regex - 排除某些单词,同时在正则表达式中包含其他模式

iphone - 如何在 self.view 的属性上设置 lldb 观察点?

iOS:用于移动设备管理的配置文件

swift - 无法识别的选择器发送到实例,通知中心获取键盘大小

ios - 将 CIImage 坐标从 CIFaceFeature 转换为 CGRect

ios - 我的 sqlite 查询(更新)在 iOS 中看起来不工作

ios - 'GADUNativeCustomTemplateAd' 错误没有可见的@interface