How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

前端 未结 13 988
攒了一身酷
攒了一身酷 2020-12-01 05:03

I\'d like a UITableView with subtitle-style cells that use dequeueReusableCellWithIdentifier.

My original Objective-C code was

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 05:33

    You can use a slightly different syntax than the one from memmons to prevent the forced unwrapping:

    let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell ?? UITableViewCell(style: .Subtitle, 
                reuseIdentifier: reuseIdentifier)
    
    cell.detailTextLabel?.text = "some text"
    
    return cell
    

    This is using XCode 6.1 7, Swift 1.2 2.0 syntax where UITableView is no longer an optional.

提交回复
热议问题