iOS:dequeueReusableCell(withIdentifier:for:) 和 dequeueReusableCell(withIdentifier:) 有什么区别? [英] iOS: What is a difference between dequeueReusableCell(withIdentifier:for:) and dequeueReusableCell(withIdentifier:)?

查看:46
本文介绍了iOS:dequeueReusableCell(withIdentifier:for:) 和 dequeueReusableCell(withIdentifier:) 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据官方文档,有两种方法可以从一个tableView的队列中获取一个可复用的cell.一个是dequeueReusableCell(withIdentifier:for:),另一个是dequeueReusableCell(withIdentifier:).假设从文档的解释来看,我认为前者是返回可重用单元格并将其添加到tableView的方法.另一方面,后者是只返回可重用单元格的方法.对吗?

According to the official documentation, there are two ways to get a reusable cell from a queue of a tableView. One is dequeueReusableCell(withIdentifier:for:) and another is dequeueReusableCell(withIdentifier:). Assuming from the explanation of the document, I think the former is the method that returns the reusable cell and adds it to tableView. On the other hand, the latter is the method that just returns the reusable cell. Is this right?

如果是对的,我还有一个问题.

If it is right, I have another question.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = self.tableView.dequeueReusableCell(with: SomeTableViewCell.self, for: indexPath)
    let anotherCell: UITableViewCell = self.tableView.dequeueReusableCell(with: AnotherTableViewCell.self)
    return anotherCell
}

在第一行,我们可以获得可重用的单元格并将cell 添加到tableView.而在第二个,我们只是获得了另一个可重复使用的单元格.最后,该方法返回从第二行获得的单元格.返回的单元格与已经添加到 tableView 的单元格不同.在这种情况下,第一行添加到 tableView 的单元格只是被最后一行返回的单元格替换了?谢谢.

At first line, we can get the reusable cell and add the cell to the tableView. And at the second one, we just obtain another reusable cell. Finally, the method returns the cell obtained from the second line. The returned cell is different with the cell being having added to the tableView already. In this case, the cell added to tableView at first line is just replaced by the returned cell at the final line? Thanks.

推荐答案

查看后 官方文档 我意识到你的解释有些正确.

After having a look at the official documentation

 I realised that your interpretation is somewhat correct.

您能否看看这个的答案,因为这可能会更加清晰.

Could you please have a look at this answer, as this might bring up more clarity.


这篇关于iOS:dequeueReusableCell(withIdentifier:for:) 和 dequeueReusableCell(withIdentifier:) 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆