1

As part of my study to understand UIKit deeply and how its functions are implemented, I was reading that in the early years of iOS, UITableView loaded every cell in the table view, but after what they do was that now loads just cells that are visible. So how could the dequeueReusableCell(withIdentifier identifier:String) be implemented?

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

What I think is that there's an array that holds visible cells and that methods just filters according to the identifier. Something like this:

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

But I want to know if there could be a better way to understand it and do it.

abnerabbey
  • 125
  • 1
  • 10
  • 1
    I think it's the opposite of what you're describing. dequeueReusableCell is looking at a collection of cells that aren't visible (i.e. can be blanked out and redrawn), and returning one of those and pulling it out of that pool. See https://stackoverflow.com/questions/3552343/uitableview-dequeuereusablecellwithidentifier-theory – aciniglio Jun 05 '20 at 02:04

1 Answers1

1

There is a project "Chameleon" created 10 years ago which objective was to implement UIKit on macOS. The author done a lot of debug / reverse engineering to understand and mimics most of UIKit types. The code is accessible on Github and UITableView implementation is 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;
}

There is also a reverse engineered version of UIKit from Microsoft but in c++ https://github.com/Microsoft/WinObjC/tree/master/Frameworks/UIKit

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93