//
// RootViewController.m
// UI10_自定义Cell
//
// Created by Marry W. on 12/18/15.
// Copyright (c) 2015 蓝鸥. All rights reserved.
//

import "RootViewController.h"

warning 1.导入cell头文件

import "MyTableViewCell.h"

import "MyTableViewCell2.h"

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSArray *arr;
@end

@implementation RootViewController

  • (void)dealloc
    {

    [_arr release];
    [super dealloc];

    }

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"自定义Cell";
    
    // tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];
    
    // 获取数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ContactList" ofType:@"plist"];
    self.arr = [NSArray arrayWithContentsOfFile:path];
    
    

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return _arr.count;
    }
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

if 0

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
}
cell.textLabel.text = _arr[indexPath.row][@"name"];
cell.detailTextLabel.text = _arr[indexPath.row][@"phone"];

endif

if 0

warning 2.使用自定义cell类型获取cell

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {

warning 3.创建自定义cell

    cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
}
NSDictionary *dic = _arr[indexPath.row];
cell.nameLabel.text = dic[@"name"];
cell.sexLabel.text = dic[@"sex"];
cell.phoneLabel.text = dic[@"phone"];
cell.hobbyLabel.text = dic[@"hobby"];

endif


if (indexPath.row % 2 != 0) {
    
    MyTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[MyTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSDictionary *dic = _arr[indexPath.row];
    cell.nameLabel.text = dic[@"name"];
    cell.sexLabel.text = dic[@"sex"];
    cell.numberLabel.text = dic[@"number"];
    cell.phoneLabel.text = dic[@"phone"];
    cell.hobbyLabel.text = dic[@"hobby"];
    
    return cell;
} else {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    if (!cell) {

warning 3.创建自定义cell

        cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"] autorelease];
    }
    NSDictionary *dic = _arr[indexPath.row];
    cell.nameLabel.text = dic[@"name"];
    cell.sexLabel.text = dic[@"sex"];
    cell.phoneLabel.text = dic[@"phone"];
    cell.hobbyLabel.text = dic[@"hobby"];
    return cell;
}

}

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
    {
    return 310;
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.
}
*/

@end



//
// MyTableViewCell.h
// UI10_自定义Cell
//
// Created by Marry W. on 12/18/15.
// Copyright (c) 2015 蓝鸥. All rights reserved.
//

import <UIKit/UIKit.h>

@interface MyTableViewCell : UITableViewCell
// 可以从父类继承来 textLabel等系统控件 命名时 注意不要和原有的重名
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *sexLabel;
@property (nonatomic, retain) UILabel *phoneLabel;
@property (nonatomic, retain) UILabel *hobbyLabel;
@end


//
// MyTableViewCell.m
// UI10_自定义Cell
//
// Created by Marry W. on 12/18/15.
// Copyright (c) 2015 . All rights reserved.
//

import "MyTableViewCell.h"

@implementation MyTableViewCell

  • (void)dealloc
    {

    [_nameLabel release];
    [_sexLabel release];
    [_hobbyLabel release];
    [_phoneLabel release];
    [super dealloc];

    }

// 重写cell的初始化

  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    // 创建控件 不设置frame
    // name
    self.nameLabel = [[UILabel alloc] init];
    _nameLabel.backgroundColor = [UIColor redColor];
    // cell中用于存放控件的父视图为contentView
    [self.contentView addSubview:_nameLabel];
    [_nameLabel release];
    // sex
    self.sexLabel = [[UILabel alloc] init];
    _sexLabel.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:_sexLabel];
    [_sexLabel release];
    // phone
    self.phoneLabel = [[UILabel alloc] init];
    _phoneLabel.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:_phoneLabel];
    [_phoneLabel release];
    // hobby
    self.hobbyLabel = [[UILabel alloc] init];
    _hobbyLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_hobbyLabel];
    [_hobbyLabel release];

    }
    return self;
    }

// 布局子视图
// 每一次cell将要显示时都会执行 是显示之前 执行的最后一个方法

  • (void)layoutSubviews
    {

    // 布局cell上系统创建的控件
    [super layoutSubviews];
    // 设置自定义cell上的控件布局
    _nameLabel.frame = CGRectMake(30, 30, self.contentView.bounds.size.width - 60, 40);
    CGRect f = _nameLabel.frame;
    f.origin.y += 70;
    _sexLabel.frame = f;
    f.origin.y += 70;
    _phoneLabel.frame = f;
    f.origin.y += 70;
    _hobbyLabel.frame = f;

    }

  • (void)awakeFromNib {

    // Initialization code

    }

  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state

    }

@end



//
// MyTableViewCell2.h
// UI10_自定义Cell
//
// Created by Marry W. on 12/18/15.
// Copyright (c) 2015 蓝鸥 rights reserved.
//

import <UIKit/UIKit.h>

@interface MyTableViewCell2 : UITableViewCell
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *sexLabel;
@property (nonatomic, retain) UILabel *phoneLabel;
@property (nonatomic, retain) UILabel *hobbyLabel;
@property (nonatomic, retain) UILabel *numberLabel;
@end


//
// MyTableViewCell2.m
// UI10_自定义Cell
//
// Created by Marry W. on 12/18/15.
// Copyright (c) 2015 蓝鸥. All rights reserved.
//

import "MyTableViewCell2.h"

@implementation MyTableViewCell2

  • (void)dealloc
    {
    [_nameLabel release];
    [_sexLabel release];
    [_phoneLabel release];
    [_numberLabel release];
    [_hobbyLabel release];
    [super dealloc];
    }
  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    // 创建控件 不设置frame
    // name
    self.nameLabel = [[UILabel alloc] init];
    _nameLabel.backgroundColor = [UIColor redColor];
    // cell中用于存放控件的父视图为contentView
    [self.contentView addSubview:_nameLabel];
    [_nameLabel release];
    // sex
    self.sexLabel = [[UILabel alloc] init];
    _sexLabel.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:_sexLabel];
    [_sexLabel release];
    // phone
    self.phoneLabel = [[UILabel alloc] init];
    _phoneLabel.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:_phoneLabel];
    [_phoneLabel release];
    // number
    self.numberLabel = [[UILabel alloc] init];
    _numberLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_numberLabel];
    [_numberLabel release];
    // hobby
    self.hobbyLabel = [[UILabel alloc] init];
    _hobbyLabel.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_hobbyLabel];
    [_hobbyLabel release];

    }
    return self;
    }

  • (void)layoutSubviews
    {
    // 布局cell上系统创建的控件
    [super layoutSubviews];
    _nameLabel.frame = CGRectMake(20, 20, 150, 40);
    CGRect f1 = _nameLabel.frame;
    f1.origin.y += 60;
    _phoneLabel.frame = f1;
    _sexLabel.frame = CGRectMake(20 + 150 + 30, 20, 150, 40);
    CGRect f2 = _sexLabel.frame;
    f2.origin.y += 60;
    _numberLabel.frame = f2;
    _hobbyLabel.frame = CGRectMake(20, 20 + 40 * 2 + 40, 30 + 150 + 150, 40);
    }
  • (void)awakeFromNib {
    // Initialization code
    }
  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    }

@end