作为我深入理解UIKit及其功能实现方式的研究的一部分,我读到在iOS的早期,UITableView加载了表视图中的每个单元,但之后,它们只加载了可见的单元格。那么如何实现dequeueReusableCell(withIdentifier标识符:String)呢?
class UITableView: UIScrollView {
func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
}
}我认为有一个数组可以保存可见的单元格,方法只是根据标识符进行过滤。就像这样:
let cell = visibleCells.filter { $0.identifier == identifier }
return cell但我想知道是否有更好的方法来理解和做它。
发布于 2020-06-05 06:09:18
10年前创建了一个名为“变色龙”的项目,其目标是在UIKit on macOS上实现。为了理解和模仿大多数UIKit类型,作者做了大量的调试/逆向工程。代码可以在Github上访问,而UITableView实现是这里。
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
{
for (UITableViewCell *cell in _reusableCells) {
if ([cell.reuseIdentifier isEqualToString:identifier]) {
UITableViewCell *strongCell = cell;
// the above strongCell reference seems totally unnecessary, but without it ARC apparently
// ends up releasing the cell when it's removed on this line even though we're referencing it
// later in this method by way of the cell variable. I do not like this.
[_reusableCells removeObject:cell];
[strongCell prepareForReuse];
return strongCell;
}
}
return nil;
}微软也有一个反向工程版本的UIKit,但在c++ https://github.com/Microsoft/WinObjC/tree/master/Frameworks/UIKit中。
https://stackoverflow.com/questions/62206914
复制相似问题