UITableView使用方法(三) - 交替颜色单元格的实现

114 篇文章 0 订阅
100 篇文章 0 订阅


效果如下:
























主要是在tableView的cellForRowAtIndexPath中实现,实现代码如下:

- (UITableViewCell *) tableView:(UITableView *)tableView
          cellForRowAtIndexPath:(NSIndexPath *)indexPath

{   
    
    NSString *tag=@"tag";
    CGSize size;
    CGFloat actualSize;
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];
    
    if (cell==nil ) {
        cell=[[[ UITableViewCell alloc ] 
               initWithStyle:UITableViewCellStyleSubtitle
               reuseIdentifier:tag] autorelease];
        
    }    
    
    NSUInteger row=[indexPath row];
     
    //设置文本
    cell.textLabel.text = [listData objectAtIndex :row];
    
    //---------------------------------------------------------
    //设置颜色交替
    if( (row%2) ==0)
        cell.backgroundColor = [UIColor whiteColor];
    else
        cell.backgroundColor = [UIColor lightGrayColor];
    //---------------------------------------------------------
    
    cell.detailTextLabel.text = [listData objectAtIndex :row];
    
    return cell;
}

比较简单,就是改变不同行的背景就行了。


运行环境:

MacOS 10.7.4 + XCode 4.3.3 + iOS 5.1


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值