UITableView in Swift

前端 未结 17 1377
滥情空心
滥情空心 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:18

    Actually in the Apple's TableView Guide document and Sample Code you will find the sentence below:

    If the dequeueReusableCellWithIdentifier: method asks for a cell that’s defined in a storyboard, the method always returns a valid cell. If there is not a recycled cell waiting to be reused, the method creates a new one using the information in the storyboard itself. This eliminates the need to check the return value for nil and create a cell manually.

    So,we could just code like this:

    var identifer: String = "myCell"
    var cell = tableView.dequeueReusableCellWithIdentifier(identifer) as UITableViewCell
    cell.textLabel.text = a[indexPath.row].name
    cell.detailTextLabel.text = "detail"
    

    I think this is a suitable way to use tableView

提交回复
热议问题