欢迎转载,转载请注明出处
本文地址:http://blog.csdn.net/zhenggaoxing/article/details/42234191
单元格的复用
原理:
一个表格中能显示多少数据呢?如果每一个单元格都要维持一个对象,那么当cell足够大的时候,IOS设备维持不了这么多cell,那么IOS中采用了复用的方式:就是屏幕上显示几个,就创建几+x个对象。当下拉的时候,检测复用队列里面是否有符合条件的对象,如果没有,返回nil ,创建新对象,如果有,就使用旧的对象。同事,划出屏幕的单元格清空,进入复用队列
解释一下:
新年快到了,央视积极准备春晚,假设节目多达60个,每个节目需要200个配角演员,那么春晚光伴舞就多达12000人!那还能不能愉快的玩耍了?倒是可以体验一把春运的感觉了。 于是乎,节目组为了提高效率。就让演员分成两组:台上组+台下组。一个节目开始了,台下组如果有合适足够的演员,就直接上,如果不够,找来再上。同时,下场的演员卸妆休息待命。
代码方式复用
// 确定Cell标识
static NSString *cellIdentifier=@"Cell";
// 复用 Cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
设置故事板属性+代码方式复用
设置标志+level=1
// 确定Cell标识 static NSString *cellIdentifier=@"Cell"; // 复用 Cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
实现讲解
测试代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataList count]; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 确定Cell标识 static NSString *cellIdentifier=@"Cell"; static int i=0; UITableViewCellStyle style; NSString *cellType; switch (indexPath.row % 4) { case 0: style = UITableViewCellStyleDefault; cellType = @"Default Style"; //有标题没有正文(没有细节文字)。可选的图片 break; case 1: style = UITableViewCellStyleSubtitle; cellType = @"Subtitle Style"; //标题和正文方式,上下排布。可选的图片 break; case 2: style = UITableViewCellStyleValue1; cellType = @"Value1 Style"; //左边文字左对齐,右边文字右对齐。可选的图片 break; case 3: style = UITableViewCellStyleValue2; cellType = @"Value2 Style"; //左边文字右对齐,蓝色;右边文字左对齐,黑色。没有图片 break; } // 复用 Cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellType]; if(cell==nil){ i++; cell=[[UITableViewCell alloc]initWithStyle:style reuseIdentifier:cellType]; NSLog(@"cell创建了%i次",i); } // 球队名称 cell.detailTextLabel.text=@"我是副标题"; cell.textLabel.text=[[dataList objectAtIndex:indexPath.row] objectForKey:@"name"]; // 根据plist文件生成 字符串 NSString *imagePath=[[[dataList objectAtIndex:indexPath.row]objectForKey:@"image"] stringByAppendingString:@".png"]; // 根据字符串加载图片 cell.imageView.image=[UIImage imageNamed:imagePath]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 152; }
测试:运行:展示了4个(创建4次)
下拉一格,因为复用区没有cell 返回nil,第五次创建,同时第一个cell进入复用区
创建8次以后(复用区集齐了四种cell),不再添加
普通表格(原理)
tableview 要动态展示Cell ,基本有两种情况(数据源展示原理):
1 展示所有Cell
2 分组展示所有Cell
用故事解释一下:
班级点名(显示Cell):
1 当班里没有组的时候,需要点:人名(Cell) 人数(Cell的数量)
2 当班主任分配了组长的时候就需要这样点名:几组(section) 组长名称:(section);接下来由组长点名:人名(X组李XX) 人数(报告老师:X组共X人)
下面上图:
代码展示:
1 几组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//小译:-(数)这个tableview里面有几个节:某tableview
2 组长名字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//小译: -(文字)哪个tableview:某tableview 这一节的节头:某section
3 该组几个人?
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//小译: -(数)哪个tableview :某tableview 这一节中的行数:某节
4 组员是谁?
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// 小译:-(单元格)哪个tableview:某tableview 这个索引路径中的行数:某索引路径
// 注:索引路径 indexPath 类型 :里面包含 section 节+row indexPath.row indexPath.section
实现普通表格
实现复用:
// 确定Cell标识
static NSString *cellIdentifier=@"Cell";
// 复用 Cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
实现数据源方法
#pragma mark-dataSource method
/*--------------------------------------班级点名---------------------------------------*/
// 第几组有几个人
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataList count];
}
// 他们都叫什么
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 确定Cell标识
static NSString *cellIdentifier=@"Cell";
// 复用 Cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
cell.detailTextLabel.text=@"这就是副标题";
// 球队名称
cell.textLabel.text=[[dataList objectAtIndex:indexPath.row] objectForKey:@"name"];
// 根据plist文件生成 字符串
NSString *imagePath=[[[dataList objectAtIndex:indexPath.row]objectForKey:@"image"] stringByAppendingString:@".png"];
// 根据字符串加载图片
cell.imageView.image=[UIImage imageNamed:imagePath];
// cell.accessoryType=UITableViewCellAccessoryNone;
cell.accessoryType=UITableViewCellAccessoryCheckmark;
// cell.accessoryType=UITableViewCellAccessoryDetailButton;
// cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
// cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
源代码:
https://git.oschina.net/zhengaoxing/IOS_TableView