UITableView in Swift

前端 未结 17 1383
滥情空心
滥情空心 2020-12-04 08:09

I\'m struggling to figure out what\'s wrong with this code snippet. This is currently working in Objective-C, but in Swift this just crashes on the first line of the method.

17条回答
  •  独厮守ぢ
    2020-12-04 08:32

    There's a few answers here, but I don't think any of them are ideal, because after the declaration you're ending up with an optional UITableViewCell, which then needs a cell!... in any declarations. I think this is a better approach (I can confirm this compiles on Xcode 6.1):

    var cell:UITableViewCell
    
    if let c = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell {
        cell = c
    }
    else {
        cell = UITableViewCell()
    }
    

提交回复
热议问题