2.使用代码自定义cell

1.1 继承UITableViewCell,在内部进行定

2.2 定义要添加的属性值如:
     @property (weak, nonatomic) IBOutlet UILabel *name;
     @property (weak, nonatomic) IBOutlet UIImageView *image; 
2.3 在- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 方法中设置属性,
2.4在// 该方法返回值决定各表格行的控件。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中初始化 

// 该方法返回值决定各表格行的控件。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 为表格行定义一个静态字符串作为标识符
    static NSString* cellId = @"cellId";
    // 从可重用表格行的队列中取出一个表格行
    FKBookTableCell* cell = [tableView
        dequeueReusableCellWithIdentifier:cellId];
    // 如果取出的表格行为nil
    if(cell == nil)
    {
        // 创建自定义的FKBookTableCell对象
        cell = [[FKBookTableCell alloc]
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    // 从IndexPath参数中获取当前行的行号
    NSUInteger rowNo = indexPath.row;
    //将单元格的边框设置为圆角
    cell.layer.cornerRadius = 12;
    cell.layer.masksToBounds = YES;
    
    // 为表格行的nameField、priceField的text设置值
    cell.nameField.text = [books objectAtIndex:rowNo];
    cell.priceField.text = [prices objectAtIndex:rowNo];
    return cell;
}
View Code

 

1.2 在外部进行注册cell标识符

1. #define kCellIdentifier_TitleRImageMore @"TitleRImageMoreCell"

2.跟tableview进行关联

- (void)loadView{

  [tableView registerClass:[TitleRImageMoreCell class] forCellReuseIdentifier:kCellIdentifier_TitleRImageMore];

}

 3.开始创建cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        TitleRImageMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TitleRImageMore forIndexPath:indexPath];
        cell.curUser = _curUser;
        [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
        return cell;
    }
View Code

 

4.继承 UITableViewCell类方法

@interface TitleRImageMoreCell : UITableViewCell
@property (strong, nonatomic) User *curUser;

+ (CGFloat)cellHeight;
@end


@interface TitleRImageMoreCell ()
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIImageView *userIconView;
@end
@implementation TitleRImageMoreCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (!_titleLabel) {
            _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, ([TitleRImageMoreCell cellHeight] -30)/2, 100, 30)];
            _titleLabel.backgroundColor = [UIColor clearColor];
            _titleLabel.font = [UIFont systemFontOfSize:16];
            _titleLabel.textColor = [UIColor blackColor];
            [self.contentView addSubview:_titleLabel];
        }
        if (!_userIconView) {
            _userIconView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreen_Width- kTitleRImageMoreCell_HeightIcon)- kPaddingLeftWidth- 30, ([TitleRImageMoreCell cellHeight] -kTitleRImageMoreCell_HeightIcon)/2, kTitleRImageMoreCell_HeightIcon, kTitleRImageMoreCell_HeightIcon)];
            [_userIconView doCircleFrame];
            [self.contentView addSubview:_userIconView];
        }
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    if (!_curUser) {
        return;
    }
    self.titleLabel.text = @"头像";
    [self.userIconView sd_setImageWithURL:[_curUser.avatar urlImageWithCodePathResizeToView:_userIconView] placeholderImage:kPlaceholderMonkeyRoundView(_userIconView)];
}

+ (CGFloat)cellHeight{
    return 70.0;
}

@end
View Code

 

 

posted on 2015-07-05 11:13  pTrack  阅读(118)  评论(0)    收藏  举报