您的位置:首页 > 产品设计 > UI/UE

定制单元格

2014-04-10 15:30 232 查看
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

{

    //创建标题文本

    _t
4000
itleLabel = [[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"];
//其他

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  布局 uitableviewcell