自定义UITableViewCell类无法在不崩溃的情况下返回枚举值

时间:2014-10-18 23:19:00

标签: ios uitableview swift

在继承我的UITableViewController的类中,我有:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as GoalTableViewCell

        cell.priority = goalsArray[indexPath.row].priority as Priority //this causes no problems.
        println(cell.priority.toRaw()) //the getter causes a crash.
        //println(goalsArray[indexPath.row].priority.toRaw()) this Does Work.
        return cell
    }

在GoalTableViewCell的课程中,我有:

var priority : Priority {
        get {
            if (self.priority == Priority.highPriority) {
                return Priority.highPriority
            } else if (self.priority == Priority.mediumPriority) {
                return Priority.mediumPriority
            } else if (self.priority == Priority.lowPriority) {
                return Priority.lowPriority
            } else {
                return Priority.defaultPriority
            }
        }

        set  {
            if (newValue == Priority.defaultPriority) {
                //action here.
            } else if (newValue == Priority.lowPriority) {
                //action here.
            } else if (newValue == Priority.mediumPriority) {
                //action here.
            } else if (newValue == Priority.highPriority) {
                //action here.
            }
        }
    }

getter无效,因为setter似乎执行正常。

我收到的错误如下:

“THREAD 1:EXC_BAD_ACCESS(code = 2,address = 0x7fff518d9ff8)” 此错误生成的控制台窗口中没有文本,应用程序完全崩溃,无法继续执行此操作。

它崩溃的行是if (self.priority == Priority.highPriority) {

我想我误解了吸气剂的一些事情?

此处还有枚举:

enum Priority : Int {
    case defaultPriority = 0
    case lowPriority = 1
    case mediumPriority = 2
    case highPriority = 3
}

编辑:实际上我无法确认setter是否正常工作,因为它错误地为我的tableView执行。如果我跑

    set  {
        if (newValue == Priority.defaultPriority) {
            self.backgroundColor = UIColor.redColor
        } else if (newValue == Priority.lowPriority) {
            //action here.
        } else if (newValue == Priority.mediumPriority) {
            //action here.
        } else if (newValue == Priority.highPriority) {
            //action here.
        }
    }
}

它实际上错误地一致地显示了单元格,有时它们都变成了红色,但情况并非如此(滚动时)。我仍然在试图确定哪里出了问题。

1 个答案:

答案 0 :(得分:1)

你正在崩溃,因为你的getter中有一个无限循环。 Swift不会像Objective-C那样为您的计算属性自动创建一个ivar。所以,当你在这里打电话给self.priority时:

var priority : Priority {
        get {
            if (self.priority == Priority.highPriority) {

你实际上只是再次打电话给你的吸气鬼。

您只需创建自己的ivar并在获取/设置器中使用它即可轻松解决此问题:

private var _priority: Priority = Priority.defaultPriority
var priority : Priority {
    get {
        if (_priority == Priority.highPriority) {
            return Priority.highPriority
        } else if (_priority == Priority.mediumPriority) {
            return Priority.mediumPriority
        } else if (_priority == Priority.lowPriority) {
            return Priority.lowPriority
        } else {
            return Priority.defaultPriority
        }
    }

    set  {
        _priority = newValue
        if (newValue == Priority.defaultPriority) {
            //action here.
        } else if (newValue == Priority.lowPriority) {
            //action here.
        } else if (newValue == Priority.mediumPriority) {
            //action here.
        } else if (newValue == Priority.highPriority) {
            //action here.
        }
    }
}

话虽这么说,我不确定你为什么在你的吸气器中得到if / else-if;简单地返回_priority应该做同样的事情:

get {
    return _priority
}

通过在标准(即非计算)属性上使用属性观察器,您实际上可以进一步简化:

var priority : Priority {
    didSet {
        if (self.priority == Priority.defaultPriority) {
            //action here.
        } else if (self.priority == Priority.lowPriority) {
            //action here.
        } else if (self.priority == Priority.mediumPriority) {
            //action here.
        } else if (self.priority == Priority.highPriority) {
            //action here.
        }
    }
}

注意:这样做,需要_priority属性,因为您没有实施客户获取和设定器。

而且,为了完整起见,我可能会使用switch语句而不是if / else-if

var priority: Priority {
    didSet {
        switch self.priority {
        case .lowPriority:
            // action here
        case .mediumPriority:
            // action here
        case .highPriority:
            // action here
        case .defaultPriority:
            // action here
        }
    }
}