I have a weird problem in a Swift + Objective-C problem.
I'm implementing a UITableView and a custom cell with a delegate in swift, but as soon as my UITableViewController assign my cell delegate to self, it crash both my app and Xcode.
Yeah each time I crash my app, Xcode crash too, no matter what, but this is another problem.
Here is a part of my cell
enum NewsCellActionType: Int {
case Vote = 0
case Comments
case Time
}
protocol NewsCellDelegate {
func newsCellDidSelectButton(cell: NewsCell, actionType: NewsCellActionType)
}
class NewsCell: UITableViewCell {
var cellDelegate: NewsCellDelegate?
func selectedAction(action: NewsCellActionType) {
self.cellDelegate?.newsCellDidSelectButton(self, actionType: action)
}
}
And here is where I set the delegate in my UIViewController
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier(NewsCellsId) as NewsCell
if cell == nil {
cell = NewsCell(style: UITableViewCellStyle.Default, reuseIdentifier: NewsCellsId)
}
cell.post = self.posts[indexPath.row] as HNPost
cell.cellDelegate = self
return cell
}
It crash at the line cell.cellDelegate = self, I have no idea why. Is it a bug in the current DP, or am I doing it wrong?
I tried to use weak on my delegate var + the @objc tag on the protocol, but as I use a pure Swift enum I can't do that. But do I need it?
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…