UITableView dequeueReusableCellWithIdentifier Theory

前端 未结 3 1433
离开以前
离开以前 2020-11-28 05:55

When apple developed the UITableView for the first iPhone they had a problem in performance when scrolling through it. Then one clever engineer discovered that

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 06:43

    The code for deqeueueReusableCellsWithIdentifier: will look something like this:

    (Taken from one of my own projects where I do something similar with views/pages in a paged scroll view)

    - (UIView*) dequeueReusablePage
    {
        UIView* page = [reusablePages_ anyObject];
        if (page != nil) {
            [[page retain] autorelease];
            [reusablePages_ removeObject: page];
        }
        return page;
    }
    

    So it keeps a simple NSMutableSet with reusable objects.

    When cells scroll off the screen and are not longer visible, they are put in this set.

    So you start with an empty set and the set will only grow if you actually have more data to show then is visible on the screen.

    Used cell scrolls off the top of the screen, is put in the set, then taken for the cell that appears at the bottom of the screen.

提交回复
热议问题