UITableViewCell 自定义

//自定义 cell
#import "RootTableViewController.h"
#import "Person.h"
#import "PersonCell.h"

@interface RootTableViewController ()
@property (nonatomic, retain) NSMutableArray *perArray;//需要添加
@end

static NSString *identifier = @"abc";
@implementation RootTableViewController
- (void)dealloc
{
    [_perArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //自定义 cell 步骤
    //1.设计 cell
    //2.选择底板(UITableViewCell)
    //3.继承于底板类, 创建子类
    //4.根据底板上的控件个数, 写属性
    //5.重写父类初始化方法, 对底板上的控件进行创建
    //6.使用 cell, 对 cell 测试

    //优化措施: 在封装的 cell 中, 封装一个方法, 对 cell 的控件进行赋值

//    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"第一个页面";

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self createData];

    //cell 封装好了, 就要改
    [self.tableView registerClass:[PersonCell class] forCellReuseIdentifier:identifier];
    self.tableView.rowHeight = 70;//行高默认为44
}

- (void)createData {
    //1.plist文件读数据
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Person" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];//最外层是数组, 数组里面放的都是对象
    //打印
    NSLog(@"%@", array);

    //2.数据封装
    self.perArray = [NSMutableArray arrayWithCapacity:0];//初始化
    for (NSMutableDictionary *dic in  array) {
        //将读取出来的数据, 装换成对象
        Person *person = [[Person alloc] initWithDictionary:dic];
        //最终都装换成数组存起来
        [self.perArray addObject:person];
        [person release];
    }
    NSLog(@"%@", self.perArray);
}

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

//3.展示
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    // Configure the cell...
    //一个区, 基本用 row 较多
    Person *person = self.perArray[indexPath.row];
    [cell showData:person];
//    cell.textLabel.text = [NSString stringWithFormat:@"%@, %@, %@", person.name, person.gender, person.age];
    return cell;
}


//PersonCell 里面的数据
//宏定义: 防止数据的修改
#import "PersonCell.h"
#import "Person.h"
//间距
#define kMargin 10
//图片大小
#define kImageSize 50
//  label 高度
#define kLabelHeight 20
//屏幕宽度
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width

@implementation PersonCell
- (void)dealloc
{
    [_photoView release];
    [_nameLabel release];
    [_genderLabel release];
    [_ageLabel release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];//回车一次, 不用再写了.
    if (self) {

        //创建视图控件
        self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(kMargin, kMargin, kImageSize, kImageSize)];
        self.photoView.backgroundColor = [UIColor redColor];
        [self addSubview:self.photoView];
        [self.photoView release];

        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * kMargin + kImageSize, kMargin, kScreenWidth - kImageSize - 3 * kMargin, kLabelHeight)];
        self.nameLabel.backgroundColor = [UIColor redColor];
        [self addSubview:self.nameLabel];
        [self.nameLabel release];

        self.genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * kMargin + kImageSize, kMargin * 2 + kLabelHeight,(self.nameLabel.frame.size.width - kMargin) / 2, kLabelHeight)];
        self.genderLabel.backgroundColor = [UIColor redColor];
        [self addSubview:self.genderLabel];
        [self.genderLabel release];

        self.ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - kMargin - self.genderLabel.frame.size.width, self.genderLabel.frame.origin.y, self.genderLabel.frame.size.width, kLabelHeight)];
        self.ageLabel.backgroundColor = [UIColor redColor];
        [self addSubview:self.ageLabel];
        [self.ageLabel release];
    }
    return self;
}

//数据的封装, 优化
- (void)showData:(Person *)person {
    self.nameLabel.text = person.name;
    self.genderLabel.text = person.gender;
    self.ageLabel.text = person.age;
}

//Person类
//Person 类
#import "Person.h"

@implementation Person

- (void)dealloc
{
    [_name release];
    [_gender release];
    [_age release];
    [super dealloc];
}

//KVC
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:otherDictionary];
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"name:%@, gender:%@, age:%@", _name, _gender, _age];
}
@end

//当然也建立了. pesonde 的 plist文件
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值