定制单元格

1.UItTableViewController
标示图控制器
两种单元格类型
UITableViewCellStyleDefault   不支持子标题
UITableViewCellStyleSubtitle   支持子标题
都支持图片与主题


另外两种单元格类型
UITableViewCellStyleValue1  
UITableViewCellStyleValue2
都不支持 图片


--------------------------定制单元格------------------------
1.通过UITableViewCell的contentView属性添加子视图
static NSString *identifier = @"firstId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]autorelease];
        /*
            不会改变的内容放在括号的里面去写
         */
        //创建子视图
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(200, 0, 100, 44)];
        label.tag = 123;
        label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
        
    }
    
    /*
         会改变的内容放在括号的外面面去写
     */
    NSDictionary *dic = [_newsList objectAtIndex:indexPath.row];


    
    //获取时间的文本
    UILabel *timeLabel = (UILabel *)[cell.contentView viewWithTag:123];
    timeLabel.text = [NSString stringWithFormat:@"%@小时之前",dic[@"time"]];
    
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@条评论",[dic objectForKey:@"commentCount"]];
    




2.使用xib自定义视图,布局十分方便,开发较为迅速
    if (cell == nil) {
        //同xib文件创建的
        /*
            用xib文件创建的时候
            一定要把UITableViewCell 的identifier属性手动的设置上
         */
        cell = [[[NSBundle mainBundle]loadNibNamed:@"SecondCell" owner:self options:nil]lastObject];
        
    }
获取工程主路径  加载xib  选择视图


3.子类化UITableViewCell,更加面向对象


- (void)_initViews
{
    //创建标题文本
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:14];
    _titleLabel.textColor = [UIColor orangeColor];
    [self.contentView addSubview:_titleLabel];
//其他视图
}
- (void)layoutSubviews
{
    //必须写的**********
    [super layoutSubviews];
    
   //进行布局
    _titleLabel.frame = CGRectMake(10, 5, 200, 20);
    _titleLabel.text = [self.dic objectForKey:@"title"];
//其他
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值