标签:row 做了 number ble ring ber -- color init
---恢复内容开始---
1.Cell的重用机制:
如下图所示:我们在写tableview的数据源方法的时候,在第三个方法中通常会碰到定义重用cell的三步骤
#pragma mark -- 数据源方法 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //1.定义重用标识符 static NSString *identifier = @"cellID"; //2.根据重用标识符去缓存区中查找重用cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //3.判断是否有重用cell if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } return cell; }
cell重用机制:如上图:最开始的时候,tableview是没有任何cell,此时缓存区也没有重用的cell,数据源方法会走(
//2.根据重用标识符去缓存区中查找重用cell
)这个方法,显然是找不到的,那么继续走第三个方法,创建一个cell,仔细看这个方法,是带有重用标识符的,这个方法做了什么呢?
如下图:这个方法给创建好的cell一个重用标识符,然后再走(
cellForRowAtIndexPath
)方法,继续创建,直到创建完成,当我们上下拖动时,就会调用缓存区中的重用cell,从而实现cell的重用.
---恢复内容结束---
标签:row 做了 number ble ring ber -- color init
原文地址:http://www.cnblogs.com/yiyuanchenfeng/p/6028915.html