OC的cell提供了一個從隊列取出使用過的cell重複利用的函數html
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];函數
經過這個函數能夠取出被回收的cell,反覆利用,從而在界面拉動的時候節約內存(tableview的cell會在界面可視範圍內對看不見的cell進行回收,不是銷燬)。工具
可是今天我測試時候故意不從隊列取,而是寫 UITableViewCell * cell=[[UITableViewCell alloc] init];測試
,而後觀察左邊內存變化狀況,發如今table拉動的時候並無增長內存。這是怎麼回事,而後我又把celll內存打印了一下優化
NSLog(@"%p",cell); spa
結果以下:code
2015-02-07 15:32:57.922 Hello[3901:117856] 0x78879070 2015-02-07 15:32:57.924 Hello[3901:117856] 0x7887b340 2015-02-07 15:32:57.925 Hello[3901:117856] 0x7887ee20 2015-02-07 15:33:02.210 Hello[3901:117856] 0x78e68c10 2015-02-07 15:33:02.744 Hello[3901:117856] 0x78e68e00 2015-02-07 15:33:09.698 Hello[3901:117856] 0x78e68f90 2015-02-07 15:33:13.476 Hello[3901:117856] 0x78879070 2015-02-07 15:33:17.094 Hello[3901:117856] 0x787792e0 2015-02-07 15:33:19.109 Hello[3901:117856] 0x78782a50 2015-02-07 15:33:20.907 Hello[3901:117856] 0x7887a500 2015-02-07 15:33:28.241 Hello[3901:117856] 0x7876e060 2015-02-07 15:33:28.491 Hello[3901:117856] 0x78782f10 2015-02-07 15:33:28.741 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:29.080 Hello[3901:117856] 0x78dc92d0 2015-02-07 15:33:29.291 Hello[3901:117856] 0x78dc8ee0 2015-02-07 15:33:29.591 Hello[3901:117856] 0x78dcb8b0 2015-02-07 15:33:29.757 Hello[3901:117856] 0x78782a50 2015-02-07 15:33:31.131 Hello[3901:117856] 0x7887f440 2015-02-07 15:33:31.209 Hello[3901:117856] 0x78782a50 2015-02-07 15:33:31.275 Hello[3901:117856] 0x78786f70 2015-02-07 15:33:31.359 Hello[3901:117856] 0x7887a500 2015-02-07 15:33:31.443 Hello[3901:117856] 0x7887f440 2015-02-07 15:33:31.591 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:31.826 Hello[3901:117856] 0x78786f70 2015-02-07 15:33:32.524 Hello[3901:117856] 0x78879070 2015-02-07 15:33:32.758 Hello[3901:117856] 0x7887bbe0 2015-02-07 15:33:32.952 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:33.091 Hello[3901:117856] 0x7887f440 2015-02-07 15:33:33.274 Hello[3901:117856] 0x78786f70 2015-02-07 15:33:33.542 Hello[3901:117856] 0x78879070 2015-02-07 15:33:33.982 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:34.124 Hello[3901:117856] 0x78dc8ee0 2015-02-07 15:33:34.275 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:34.502 Hello[3901:117856] 0x78782820 2015-02-07 15:33:34.674 Hello[3901:117856] 0x78e67e20 2015-02-07 15:33:34.775 Hello[3901:117856] 0x78e6c3c0 2015-02-07 15:33:35.692 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:35.842 Hello[3901:117856] 0x78879070 2015-02-07 15:33:36.042 Hello[3901:117856] 0x78dc8ee0 2015-02-07 15:33:36.325 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:36.476 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:36.642 Hello[3901:117856] 0x78879070 2015-02-07 15:33:36.891 Hello[3901:117856] 0x78dc8ee0 2015-02-07 15:33:39.724 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:40.108 Hello[3901:117856] 0x78e67e20 2015-02-07 15:33:40.375 Hello[3901:117856] 0x78dcabb0 2015-02-07 15:33:40.474 Hello[3901:117856] 0x78e6c440 2015-02-07 15:33:40.557 Hello[3901:117856] 0x78782820 2015-02-07 15:33:40.691 Hello[3901:117856] 0x7866bfe0 2015-02-07 15:33:40.974 Hello[3901:117856] 0x78879070 2015-02-07 15:33:41.606 Hello[3901:117856] 0x78782820 2015-02-07 15:33:42.258 Hello[3901:117856] 0x78782820 2015-02-07 15:33:42.443 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:42.527 Hello[3901:117856] 0x78786f70 2015-02-07 15:33:42.610 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:42.711 Hello[3901:117856] 0x78879070 2015-02-07 15:33:42.844 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:43.010 Hello[3901:117856] 0x78e67e20 2015-02-07 15:33:43.277 Hello[3901:117856] 0x7887f440 2015-02-07 15:33:44.261 Hello[3901:117856] 0x78e6c3c0 2015-02-07 15:33:44.346 Hello[3901:117856] 0x78782820 2015-02-07 15:33:44.411 Hello[3901:117856] 0x78786f70 2015-02-07 15:33:44.513 Hello[3901:117856] 0x78782a50 2015-02-07 15:33:44.613 Hello[3901:117856] 0x787872f0 2015-02-07 15:33:44.762 Hello[3901:117856] 0x78879070 2015-02-07 15:33:44.946 Hello[3901:117856] 0x78782820 2015-02-07 15:33:45.960 Hello[3901:117856] 0x7887f440 2015-02-07 15:33:46.277 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:46.377 Hello[3901:117856] 0x78782820 2015-02-07 15:33:46.477 Hello[3901:117856] 0x78879070 2015-02-07 15:33:46.610 Hello[3901:117856] 0x78dbd8c0 2015-02-07 15:33:46.793 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:47.060 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:47.443 Hello[3901:117856] 0x78879070 2015-02-07 15:33:47.510 Hello[3901:117856] 0x78782820 2015-02-07 15:33:47.576 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:47.677 Hello[3901:117856] 0x78dbd8c0 2015-02-07 15:33:47.810 Hello[3901:117856] 0x787872f0 2015-02-07 15:33:47.993 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:48.894 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:48.993 Hello[3901:117856] 0x78dc8fc0 2015-02-07 15:33:49.161 Hello[3901:117856] 0x78782820 2015-02-07 15:33:49.378 Hello[3901:117856] 0x78dbd8c0 2015-02-07 15:33:49.794 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:50.620 Hello[3901:117856] 0x78879070 2015-02-07 15:33:50.711 Hello[3901:117856] 0x78dc81f0 2015-02-07 15:33:50.794 Hello[3901:117856] 0x78e67060 2015-02-07 15:33:50.894 Hello[3901:117856] 0x78e67e20 2015-02-07 15:33:51.027 Hello[3901:117856] 0x78879070
用工具查找其中幾個地址就會發現有重複的,說明OC已經對這個cell的存取進行了內存優化,即便不從隊列裏面拿去也不會佔用更多內存使用,目前發現是這樣,可是建議仍是繼續用htm
dequeueReusableCellWithIdentifier 函數,讓程序更可靠。隊列