为什么UITableViewCells有可能在一行中为零?

时间:2016-04-17 18:15:05

标签: ios swift uitableview

我目前正在通过Ray Wunderlich书中学习UITableView的内部工作,并注意到在这个protocal的函数体中它使用可选的绑定来创建一个单元格。

override func tableView(tableView:  UITableView , didSelectRowAtIndexPath indexPath: NSIndexPath) {
  if let cell = tableView.cellForRowAtIndexPath(indexPath) {
    if cell.accessoryType == .None {
      cell. accessoryType = .Checkmark
    } else  {
      cell.accessoryType = .None
    }
  }

  tableView. deselectRowAtIndexPath (indexPath, animated: true )
}

可选绑定的概念对我来说仍然相当新,所以大声思考我假设不会成为每一行中的一个单元格(即返回{{1} })

这是我陷入困境的地方。在我的故事板中,我有一个表视图控制器和一个原型单元。我不应该在表视图中有这个原型单元格,这样可以确保创建多少行吗?

1 个答案:

答案 0 :(得分:0)

func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell? UITableView方法被定义为返回一个可选项,这就是你必须首先打开它的原因。

根据the docsAn object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.