Objective-c

自定义cell

2017-08-16  本文已影响0人  文艺小年青
UItableViewCell 的自定义

在学习中,我们知道了cell的三种样式,但是在实际编写中,很多时候我们需要用到不同的cell样式,这时候我们就需要自定义cell。

UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //tableview的代理方法。所以要在上边遵守它的协议。
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 200.f;
    [self.view addSubview:tableView];

UITableViewDelegate,
UITableViewDataSource

@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *gender;
@property(nonatomic,retain)NSString *phoneNumber;
@property(nonatomic,retain)NSString *contactImageName;

-(void)getData {
    //创建联系人对象,加到数组中
    Contact *contact1 = [[Contact alloc] init];
    contact1.name = @"小明";
    contact1.gender = @"男";
    contact1.phoneNumber = @"121212";
    contact1.contactImageName = @"1.jpg";
    
    Contact *contact2 = [[Contact alloc] init];
    contact2.name = @"小红";
    contact2.gender = @"女";
    contact2.phoneNumber = @"121212123";
    contact2.contactImageName = @"3.jpg";
    
    self.contactArray = @[contact1,contact2];
}

*不要忘了tableView的两个必须实现的代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contactArray.count;  
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //从数组取出的对象
    Contact *contact = self.contactArray[indexPath.row];
    //static 只初始化一次
    static NSString *contactIdentifier = @"contact";
    //用我们自定义的cell,
    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:contactIdentifier];
    if (nil == cell) {
        cell = [[ContactCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactIdentifier];
    }
    //左边的contact 是cell中的,右边的是从数组中取出的联系人对象
    cell.contact = contact;
    
    return cell;
}

@interface ContactCell ()
@property(nonatomic,retain)UIImageView * contactImageView;
@property(nonatomic,retain)UILabel * nameLable;
@property(nonatomic,retain)UILabel * genderLable;
@property(nonatomic,retain)UILabel * phoneNumberLable;
@end

//重写cell的方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.contactImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.contactImageView.backgroundColor = [UIColor redColor];
        //contentView是cell自带的view,
        [self.contentView addSubview:self.contactImageView];
        
        self.nameLable = [[UILabel alloc] initWithFrame:CGRectZero];
        self.nameLable.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.nameLable];
        
        self.genderLable = [[UILabel alloc] initWithFrame:CGRectZero];
        self.genderLable.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:self.genderLable];
        
        self.phoneNumberLable = [[UILabel alloc] initWithFrame:CGRectZero];
        self.phoneNumberLable.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.phoneNumberLable];
        
    }
    return self;
}

//在这个方法写子视图的布局代码
-(void)layoutSubviews {
    //调用父类的方法
    [super layoutSubviews];
    self.contactImageView.frame = CGRectMake(10, 10, 100, 120);
    
    self.nameLable.frame = CGRectMake(self.contactImageView.frame.origin.x + self.contactImageView.frame.size.width + 10, self.contactImageView.frame.origin.y, 100, 30);
    
    CGRect genderFrame = self.nameLable.frame;
    //调整genderFrame的y值
    genderFrame.origin.y = self.nameLable.frame.origin.y + self.nameLable.frame.size.height + 5;

    //最后把调整好的frame赋给genderLable的frame
    self.genderLable.frame = genderFrame;
    
    CGRect phoneNumberFrame = genderFrame;
    phoneNumberFrame.origin.y = genderFrame.origin.y + genderFrame.size.height + 5;
    self.phoneNumberLable.frame = phoneNumberFrame;
}
//重写setter方法。
-(void)setContact:(Contact *)contact {
    if (_contact != contact) {
        _contact = contact;
        //给视图赋值,
        self.nameLable.text = contact.name;
        self.genderLable.text = contact.gender;
        self.phoneNumberLable.text = contact.phoneNumber;
        self.contactImageView.image = [UIImage imageNamed:contact.contactImageName];
    }
    //注意:这里不能写成 self.contact = contact;  因为这样会无限的调用自己,叫做递归;
}

上一篇 下一篇

猜你喜欢

热点阅读