通过 Apple 官方文档,以及 Sample Code 中 tableView:cellForRowAtIndexPath: 的实现代码,对 dequeueReusableCellWithIdentifier: 的理解是:首次调用该方法返回 nil,然后以其参数指定的 identifier 作为 reuseIdentifier 新创建一个 UITableViewCell 对象,再然后每次调用它都返回刚创建的那个 cell。
但是,事实不是这样的!
-[UITableView dequeueReusableCellWithIdentifier:] 的目的是节约内存。比如,假设屏幕同时可显示 4 个或 5 个 cell,那么,在一个 1000 行的 table 中,系统只需要分配 4 个或 5 个 cell 对象的内存空间,而不是 1000 个,也不是 1 个。
做了个实验,创建了个 table,有 20 个 section,每个 section 包含 20 行,每行高 100 逻辑单位。分别用代码创建或者从 Nib 中加载 cell 的方式,对比对 cell 是否(正确)使用 reuseIdentifier 时内存中总共创建的 cell 数目,可以看出:不使用 reuseIdentifier 时,每次都会新创建一个 cell,当 table 的 entry 特别多时,就非常耗内存;而正确地使用了 reuseIdentifier 后,只在开始时在内存中创建了 6 个 cell,然后循环使用它们。
1)不使用 reuseIdentifier
Section: 0 Row: 0 Count: 1 Index in cellsArray: 0 Section: 0 Row: 1 Count: 2 Index in cellsArray: 1 Section: 0 Row: 2 Count: 3 Index in cellsArray: 2 Section: 0 Row: 3 Count: 4 Index in cellsArray: 3 Section: 0 Row: 4 Count: 5 Index in cellsArray: 4 Section: 0 Row: 5 Count: 6 Index in cellsArray: 5 Section: 0 Row: 6 Count: 7 Index in cellsArray: 6 Section: 0 Row: 7 Count: 8 Index in cellsArray: 7 Section: 0 Row: 8 Count: 9 Index in cellsArray: 8 Section: 0 Row: 9 Count: 10 Index in cellsArray: 9 Section: 0 Row: 10 Count: 11 Index in cellsArray: 10 Section: 0 Row: 11 Count: 12 Index in cellsArray: 11 Section: 0 Row: 12 Count: 13 Index in cellsArray: 12 Section: 0 Row: 13 Count: 14 Index in cellsArray: 13 Section: 0 Row: 14 Count: 15 Index in cellsArray: 14 Section: 0 Row: 15 Count: 16 Index in cellsArray: 15 Section: 0 Row: 16 Count: 17 Index in cellsArray: 16 Section: 0 Row: 17 Count: 18 Index in cellsArray: 17 Section: 0 Row: 18 Count: 19 Index in cellsArray: 18 Section: 0 Row: 19 Count: 20 Index in cellsArray: 19 Section: 1 Row: 0 Count: 21 Index in cellsArray: 20 Section: 1 Row: 1 Count: 22 Index in cellsArray: 21 Section: 1 Row: 2 Count: 23 Index in cellsArray: 22 Section: 1 Row: 3 Count: 24 Index in cellsArray: 23 Section: 1 Row: 4 Count: 25 Index in cellsArray: 24 Section: 1 Row: 5 Count: 26 Index in cellsArray: 25 Section: 1 Row: 6 Count: 27 Index in cellsArray: 26 Section: 1 Row: 7 Count: 28 Index in cellsArray: 27 Section: 1 Row: 8 Count: 29 Index in cellsArray: 28 Section: 1 Row: 9 Count: 30 Index in cellsArray: 29 Section: 1 Row: 10 Count: 31 Index in cellsArray: 30 Section: 1 Row: 11 Count: 32 Index in cellsArray: 31 Section: 1 Row: 12 Count: 33 Index in cellsArray: 32 Section: 1 Row: 13 Count: 34 Index in cellsArray: 33 Section: 1 Row: 14 Count: 35 Index in cellsArray: 34 Section: 1 Row: 15 Count: 36 Index in cellsArray: 35 Section: 1 Row: 16 Count: 37 Index in cellsArray: 36 Section: 1 Row: 17 Count: 38 Index in cellsArray: 37 Section: 1 Row: 18 Count: 39 Index in cellsArray: 38 Section: 1 Row: 19 Count: 40 Index in cellsArray: 39 Section: 2 Row: 0 Count: 41 Index in cellsArray: 40 Section: 2 Row: 1 Count: 42 Index in cellsArray: 41 Section: 2 Row: 2 Count: 43 Index in cellsArray: 42 Section: 2 Row: 3 Count: 44 Index in cellsArray: 43 Section: 2 Row: 4 Count: 45 Index in cellsArray: 44 Section: 2 Row: 5 Count: 46 Index in cellsArray: 45 Section: 2 Row: 6 Count: 47 Index in cellsArray: 46 Section: 2 Row: 7 Count: 48 Index in cellsArray: 47 Section: 2 Row: 8 Count: 49 Index in cellsArray: 48 Section: 2 Row: 9 Count: 50 Index in cellsArray: 49 Section: 2 Row: 10 Count: 51 Index in cellsArray: 50 Section: 2 Row: 11 Count: 52 Index in cellsArray: 51 Section: 2 Row: 12 Count: 53 Index in cellsArray: 52 Section: 2 Row: 13 Count: 54 Index in cellsArray: 53 Section: 2 Row: 14 Count: 55 Index in cellsArray: 54 Section: 2 Row: 15 Count: 56 Index in cellsArray: 55 Section: 2 Row: 16 Count: 57 Index in cellsArray: 56 Section: 2 Row: 17 Count: 58 Index in cellsArray: 57 Section: 2 Row: 18 Count: 59 Index in cellsArray: 58 Section: 2 Row: 19 Count: 60 Index in cellsArray: 59 Section: 3 Row: 0 Count: 61 Index in cellsArray: 60 Section: 3 Row: 1 Count: 62 Index in cellsArray: 61 Section: 3 Row: 2 Count: 63 Index in cellsArray: 62 Section: 3 Row: 3 Count: 64 Index in cellsArray: 63 |
2)正确使用 reuseIdentifier
Section: 0 Row: 0 Count: 1 Index in cellsArray: 0 Section: 0 Row: 1 Count: 2 Index in cellsArray: 1 Section: 0 Row: 2 Count: 3 Index in cellsArray: 2 Section: 0 Row: 3 Count: 4 Index in cellsArray: 3 Section: 0 Row: 4 Count: 5 Index in cellsArray: 4 Section: 0 Row: 5 Count: 6 Index in cellsArray: 5 Section: 0 Row: 6 Count: 6 Index in cellsArray: 0 Section: 0 Row: 7 Count: 6 Index in cellsArray: 1 Section: 0 Row: 8 Count: 6 Index in cellsArray: 2 Section: 0 Row: 9 Count: 6 Index in cellsArray: 3 Section: 0 Row: 10 Count: 6 Index in cellsArray: 4 Section: 0 Row: 11 Count: 6 Index in cellsArray: 5 Section: 0 Row: 12 Count: 6 Index in cellsArray: 0 Section: 0 Row: 13 Count: 6 Index in cellsArray: 1 Section: 0 Row: 14 Count: 6 Index in cellsArray: 2 Section: 0 Row: 15 Count: 6 Index in cellsArray: 3 Section: 0 Row: 16 Count: 6 Index in cellsArray: 4 Section: 0 Row: 17 Count: 6 Index in cellsArray: 5 Section: 0 Row: 18 Count: 6 Index in cellsArray: 0 Section: 0 Row: 19 Count: 6 Index in cellsArray: 1 Section: 1 Row: 0 Count: 6 Index in cellsArray: 2 Section: 1 Row: 1 Count: 6 Index in cellsArray: 3 Section: 1 Row: 2 Count: 6 Index in cellsArray: 4 Section: 1 Row: 3 Count: 6 Index in cellsArray: 5 Section: 1 Row: 4 Count: 6 Index in cellsArray: 0 Section: 1 Row: 5 Count: 6 Index in cellsArray: 1 Section: 1 Row: 6 Count: 6 Index in cellsArray: 2 Section: 1 Row: 7 Count: 6 Index in cellsArray: 3 Section: 1 Row: 8 Count: 6 Index in cellsArray: 4 Section: 1 Row: 9 Count: 6 Index in cellsArray: 5 Section: 1 Row: 10 Count: 6 Index in cellsArray: 0 Section: 1 Row: 11 Count: 6 Index in cellsArray: 1 Section: 1 Row: 12 Count: 6 Index in cellsArray: 2 Section: 1 Row: 13 Count: 6 Index in cellsArray: 3 Section: 1 Row: 14 Count: 6 Index in cellsArray: 4 Section: 1 Row: 15 Count: 6 Index in cellsArray: 5 Section: 1 Row: 16 Count: 6 Index in cellsArray: 0 Section: 1 Row: 17 Count: 6 Index in cellsArray: 1 Section: 1 Row: 18 Count: 6 Index in cellsArray: 2 Section: 1 Row: 19 Count: 6 Index in cellsArray: 3 Section: 2 Row: 0 Count: 6 Index in cellsArray: 4 Section: 2 Row: 1 Count: 6 Index in cellsArray: 5 Section: 2 Row: 2 Count: 6 Index in cellsArray: 0 Section: 2 Row: 3 Count: 6 Index in cellsArray: 1 Section: 2 Row: 4 Count: 6 Index in cellsArray: 2 Section: 2 Row: 5 Count: 6 Index in cellsArray: 3 Section: 2 Row: 6 Count: 6 Index in cellsArray: 4 Section: 2 Row: 7 Count: 6 Index in cellsArray: 5 Section: 2 Row: 8 Count: 6 Index in cellsArray: 0 Section: 2 Row: 9 Count: 6 Index in cellsArray: 1 Section: 2 Row: 10 Count: 6 Index in cellsArray: 2 Section: 2 Row: 11 Count: 6 Index in cellsArray: 3 Section: 2 Row: 12 Count: 6 Index in cellsArray: 4 Section: 2 Row: 13 Count: 6 Index in cellsArray: 5 Section: 2 Row: 14 Count: 6 Index in cellsArray: 0 Section: 2 Row: 15 Count: 6 Index in cellsArray: 1 Section: 2 Row: 16 Count: 6 Index in cellsArray: 2 Section: 2 Row: 17 Count: 6 Index in cellsArray: 3 Section: 2 Row: 18 Count: 6 Index in cellsArray: 4 Section: 2 Row: 19 Count: 6 Index in cellsArray: 5 Section: 3 Row: 0 Count: 6 Index in cellsArray: 0 Section: 3 Row: 1 Count: 6 Index in cellsArray: 1 Section: 3 Row: 2 Count: 6 Index in cellsArray: 2 Section: 3 Row: 3 Count: 6 Index in cellsArray: 3 |
【备注】打印结果的 4 列数字分别表示:当前 section 序数、当前 row 在 section 中的序号、至当前总共创建的 cell 数目、当前行使用的 cell 是哪个(为了方便实验,代码中维护了一个数组,用来保存创建的 cell 对象)。
【代码】dequeueReusableCellWithIdentifier.zip