【问题标题】:UIKit UITableViewCell ImplementationUIKit UITableViewCell 实现
【发布时间】:2020-06-05 01:57:11
【问题描述】:

作为我深入了解 UIKit 及其功能如何实现的研究的一部分,我读到在 iOS 的早期,UITableView 加载了表格视图,但在他们所做的是现在只加载可见的单元格。 那么dequeueReusableCell(withIdentifier identifier:String)怎么实现呢?

class UITableView: UIScrollView {
    func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
    }
}

我认为有一个包含可见单元格的数组,并且这些方法只是根据标识符进行过滤。像这样的:

let cell = visibleCells.filter { $0.identifier == identifier }
return cell

但我想知道是否有更好的方法来理解它并做到这一点。

【问题讨论】:

  • 我认为这与您所描述的相反。 dequeueReusableCell 正在查看一组不可见的单元格(即可以空白和重绘),然后返回其中一个并将其拉出该池。见stackoverflow.com/questions/3552343/…

标签: ios swift uitableview uikit


【解决方案1】:

10 年前创建了一个项目“Chameleon”,其目标是在 macOS 上实现 UIKit。作者做了很多调试/逆向工程来理解和模仿大多数 UIKit 类型。代码可在Github 访问,UITableView 实现为here

- (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;
}

还有一个来自 Microsoft 的 UIKit 逆向工程版本,但使用 c++ https://github.com/Microsoft/WinObjC/tree/master/Frameworks/UIKit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多