2017-02-17 76 views
1

操作方法是:UITableView方法和等效的UITableViewDatasource方法有什么区别?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

不同:

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?

最近我遇到了我在那里延伸UITableView能够通过排在一个给定的部分进行迭代的错误。

func forRows(inSection section: Int, condition: (UITableViewCell) ->()) { 

    for row in 0..<numberOfRows(inSection: section) { 
     let cell = cellForRow(at: IndexPath(row: row, section: section))! 

     condition(cell) 
    } 
} 

^^^不起作用,细胞是最后一排

func forRows(inSection section: Int, condition: (UITableViewCell) ->()) { 

    for row in 0..<dataSource!.tableView(self, numberOfRowsInSection: section) { 
     let cell = dataSource!.tableView(self, cellForRowAt: IndexPath(row: row, section: section)) 

     condition(cell) 
    } 
} 

^^^适用于零。我明白了,但我不明白。

TableView委托方法和tableView本身的等价方法在内部有什么不同?

回答

2

数据源是为了提供细胞的细胞,创建它们不过你想要创建它们。通常这涉及将可重复使用的细胞排出或创建新的细胞。

当你在表格视图上调用cellForRow(at indexPath: IndexPath)时,它会给你一个已经存在的单元格(如果有的话)。

+0

而'UITableView cellForRow(at:)'调用返回的单元格是通过调用其数据源上的'tableView(_:cellForRowAt:)'获得的表视图的单元格。 – rmaddy

+0

感谢您的澄清,在问题中搞砸了,纠正它以反映这一点。 –

1
  • 一个委托方法(严格来说UITableViewDataSource也是委托)是由框架打电话来问的东西或指示工作流程中的一个特定阶段。
    你不能自己调用​​委托方法

  • A 类或实例方法可以不关心地使用。

0

我不知道这是否是您的具体情况,因为我需要更多的信息,但

func cellForRow(at indexPath: IndexPath) -> UITableViewCell? 

返回nil如果电池是不是在屏幕上。 在另一种方法表将“负荷”为你在特定的索引路径

相关问题