首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIKit UITableViewCell实现

UIKit UITableViewCell实现
EN

Stack Overflow用户
提问于 2020-06-05 01:57:11
回答 1查看 92关注 0票数 1

作为我深入理解UIKit及其功能实现方式的研究的一部分,我读到在iOS的早期,UITableView加载了表视图中的每个单元,但之后,它们只加载了可见的单元格。那么如何实现dequeueReusableCell(withIdentifier标识符:String)呢?

代码语言:javascript
复制
class UITableView: UIScrollView {
    func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
    }
}

我认为有一个数组可以保存可见的单元格,方法只是根据标识符进行过滤。就像这样:

代码语言:javascript
复制
let cell = visibleCells.filter { $0.identifier == identifier }
return cell

但我想知道是否有更好的方法来理解和做它。

EN

回答 1

Stack Overflow用户

发布于 2020-06-05 06:09:18

10年前创建了一个名为“变色龙”的项目,其目标是在UIKit on macOS上实现。为了理解和模仿大多数UIKit类型,作者做了大量的调试/逆向工程。代码可以在Github上访问,而UITableView实现是这里

代码语言:javascript
复制
- (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中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62206914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档