YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

TableViewCell自定义Cell

//
// 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

}

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;
}

}

/*
#pragma mark - Navigation

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

// 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

// 重写cell的初始化

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

@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

@end

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »