IOS学习之——表视图2 实现简单的表示图

欢迎转载,转载请注明出处

本文地址: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




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值