【发布时间】:2014-06-10 09:12:41
【问题描述】:
我在 Swift + Objective-C 问题中遇到了一个奇怪的问题。
我正在快速实现一个 UITableView 和一个带有委托的自定义单元格,但是一旦我的 UITableViewController 将我的单元格委托分配给自己,它就会使我的应用程序和 Xcode 崩溃。 是的,每次我的应用程序崩溃时,Xcode 也会崩溃,无论如何,但这是另一个问题。
这是我牢房的一部分
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)
}
}
这是我在 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
}
它在 cell.cellDelegate = self 行崩溃,我不知道为什么。是当前 DP 的 bug,还是我做错了?
我尝试在我的委托 var + 协议上的 @objc 标记上使用弱,但由于我使用的是纯 Swift 枚举,所以我不能这样做。但我需要它吗?
谢谢!
【问题讨论】:
-
你自己确认 'NewsCellDelegate' 协议了吗???
-
当然可以,但我认为这是因为我的协议仅在 swift 中,并且我的类与 Objective-c 接口
-
UITableView in Swift 的可能重复项
-
这实际上是一个已经回答的问题的副本,我猜。
as与as?的问题。 -
不抱歉,这是关于委托和崩溃的。不是关于 UITableViewCell。这只是示例。
标签: ios uitableview delegates swift