自定义cell(17-08-14)

//
//  ViewController.m
//  UI07_自定义cell
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "ContactCell.h"

@interface ViewController () 

@property (nonatomic, retain) NSArray *contactArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self getData];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 200.f;
    [self.view addSubview:tableView];
}

- (void)getData {
    Contact *contact1 = [[Contact alloc] init];
    contact1.name = @"佟湘玉";
    contact1.gender = @"女";
    contact1.phoneNumber = @"132456";
    contact1.contactImageName = @"1.png";
    
    Contact *contact2 = [[Contact alloc] init];
    contact2.name = @"莫小贝";
    contact2.gender = @"女";
    contact2.phoneNumber = @"748445";
    contact2.contactImageName = @"6.png";
    
    self.contactArray = @[contact1,contact2];
    
}

- (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 NSString *contactIdentifier = @"contact";
    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:contactIdentifier];
    if (nil == cell) {
        cell = [[ContactCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactIdentifier];
    }
    cell.contact = contact;
    [cell setContact:contact];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//
//  ContactCell.h
//  UI07_自定义cell
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
// 联系人的cell

#import 
#import "Contact.h"

@interface ContactCell : UITableViewCell

@property (nonatomic, retain) Contact *contact;

@end

//
//  ContactCell.m
//  UI07_自定义cell
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "ContactCell.h"

@interface ContactCell ()

@property (nonatomic, retain) UIImageView *contentImageView;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *genderLabel;
@property (nonatomic, retain) UILabel *phoneNumberLabel;

@end

@implementation ContactCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.contentImageView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.contentImageView];
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.nameLabel.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:self.nameLabel];
        
        self.genderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.genderLabel.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:self.genderLabel];
        
        self.phoneNumberLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.phoneNumberLabel.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:self.phoneNumberLabel];
        
    }
    return self;
}
//在这个方法写子视图的布局代码(多次调用)
- (void)layoutSubviews {
    //重写父类方法
    [super layoutSubviews];
    self.contentImageView.frame = CGRectMake(10, 10, 80, 120);
    self.nameLabel.frame = CGRectMake(self.contentImageView.frame.origin.x + self.contentImageView.frame.size.width +10, self.contentImageView.frame.origin.y, 120, 30);
    CGRect genderFrame = self.nameLabel.frame;
    genderFrame.origin.y = self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height +5;
    self.genderLabel.frame = genderFrame;
    
    CGRect phoneNumberFrame = genderFrame;
    phoneNumberFrame.origin.y = genderFrame.origin.y + genderFrame.size.height +5;
    self.phoneNumberLabel.frame = phoneNumberFrame;
    
}
- (void)setContact:(Contact *)contact {
        if (_contact != contact) {
            _contact = contact;
            self.nameLabel.text = contact.name;
            self.genderLabel.text = contact.gender;
            self.phoneNumberLabel.text = contact.phoneNumber;
            self.contentImageView.image = [UIImage imageNamed:contact.contactImageName];
        }
        
    }


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

//
//  Contact.h
//  UI07_自定义cell
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import 

@interface Contact : NSObject

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

@end

你可能感兴趣的:(自定义cell(17-08-14))