自定义UItableviewCell内容

首先创建一个继承于 UITableViewCell的cell  本人例子代码如下
自定义cell里面有三个 UILabel和一个 UIImageView
ListringCell.h文件

#import <UIKit/UIKit.h>


@interface ListringCell : UITableViewCell

{

    UILabel * _name;

    UILabel * _time;

    UILabel * _title;

    UIImageView * _touxiang;

}

@property(nonatomic,retain)UILabel * _name;

@property(nonatomic,retain)UILabel * _time;

@property(nonatomic,retain)UILabel * _title;

@property(nonatomic,retain)UIImageView * _touxiang;


@end


ListringCell.m文件

#import "ListringCell.h"


@implementation ListringCell

@synthesize _name,_time,_touxiang,_title;

-(void)dealloc{

    self._name=nil;

    self._time=nil;

    self._title=nil;

    self._touxiang=nil;

    [super dealloc];

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // Initialization code

//创建三个lable和一个imageview 并添加到cell

        UIColor * col=[UIColor scrollViewTexturedBackgroundColor];

        _name=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 50, 20)];

        _time=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 270, 20)];

        _title=[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 270, 50)];

        _touxiang=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

        _touxiang.frame=CGRectMake(0, 0, 50, 50);

        _time.backgroundColor=col;

        _name.backgroundColor=col;

        _title.backgroundColor=col;

        //name字体自适应大小  以及居中显示

        _name.adjustsFontSizeToFitWidth = YES;

        _name.textAlignment = NSTextAlignmentCenter;

        _title.textAlignment = NSTextAlignmentCenter;

        _time.textAlignment = NSTextAlignmentCenter;

        [self addSubview:_name];

        [self addSubview:_time];

        [self addSubview:_touxiang];

        [self addSubview:_title];

        [_title release];

        [_touxiang release];

        [_name release];

        [_time release];

    }

    return self;

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}


定义好自己的cell后就需要在你的UItableviewController里面的方法 - ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath 使用自定义cell  代码如下:

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

{

    static NSString *CellIdentifier = @"Cell";

//用自己的自定义cell类创建cell 

    ListringCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil) {

        cell =[[[ListringCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }

    // Configure the cell...

//这里让自定义cell的lable和imageview放入自己需要放的东西

    [cell _name ].text=(NSString *)[[tableData objectAtIndex:[indexPath row]]objectForKey:@"nickname"];

    [cell _time ].text=(NSString *)[[tableData objectAtIndex:[indexPath row]]objectForKey:@"time"];

    [cell _title ].text=(NSString *)[[tableData objectAtIndex:[indexPath row]]objectForKey:@"title"];

    cell._touxiang.image=[UIImage imageNamed:[[tableData objectAtIndex:[indexPath row]] objectForKey:@"touxiang"]];

    return cell;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值