gpt4 book ai didi

ios - dequeueReusableCellWithIdentifier :forIndexPath: get used?的indexPath在哪里

转载 作者:IT王子 更新时间:2023-10-29 07:40:30 25 4
gpt4 key购买 nike

Apple 的文档中提到 indexPath 参数:

The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cell’s position in the table view.

但是 register(Class|Nib):forCellReuseIdentifier: 只指定要使用的重用标识符,而不是部分或一组索引路径。

我想也许 UITableViewCell 有某种方法可以获取索引路径,所以它可以,比如说,如果在一个部分的第一行,就可以绕过它的角落,但我没有看到它。在创建时,它得到的只是它的样式和重用标识符 (initWithStyle:reuseIdentifier:);在重用时,它只被告知 prepareForReuse

鉴于旧的 dequeueReusableCellWithIdentifier: 仍然受支持,如果它不能依赖有机会这样做的话,它可能正在做什么样的基于索引路径的配置,无论如何?

我查看了 Table View Programming Guide,但它自 iOS 5 以来一直没有更新。

最佳答案

dequeueReusableCellWithIdentifier:dequeueReusableCellWithIdentifier:indexPath: 之间最重要的区别是它们是不同的方法!因此他们可以有不同的行为,他们也确实如此。这与 indexPath 无关,真的;我们只需要一种方法来区分它们。

新方式

特别是,如果您调用 dequeueReusableCellWithIdentifier:indexPath:,这表明您正在使用新的 iOS 6 注册和出列系统。所以,如果你没有注册这个标识符,你会得到一个很好的崩溃和解释问题的日志消息。这个方法永远不会返回 nil;它总是返回一个单元格,要么通过创建一个新单元格,要么通过重复使用一个单元格。

旧方法

另一方面,简单明了的 dequeueReusableCellWithIdentifier: 是旧的,必须向后兼容。如果你还没有注册这个标识符,它不会提示:它只会返回 nil,让你无所适从。你必须自己创建单元格,就像过去的糟糕日子一样。

编辑:但另请参阅@svena 的回答!新方法(使用 indexPath:)还有第二个我不知道的优势:单元格在返回给您时大小正确。

关于ios - dequeueReusableCellWithIdentifier :forIndexPath: get used?的indexPath在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12714737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com