@implementation HomeTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"default Cell";
break;
case 1:
cell.textLabel.text = @"1 concentView";
break;
case 2:
cell.textLabel.text = @"2 Nib";
break;
case 3:
cell.textLabel.text = @"3 custom Cell";
break;
default:
break;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailTableViewController *detailTVC = [[DetailTableViewController alloc]init];
//记录选中的内容
detailTVC.cellType = indexPath.row;
[self.navigationController pushViewController:detailTVC animated:YES];
}
#import <UIKit/UIKit.h>
#import "HomeTableViewController.h"
#import "MyCell.h"
//添加玫举
typedef enum kTableViewCellType{
kDefualtCellType = 0,
kConcentCellType = 1,
kNibCellType = 2,
kCustomCellType = 3
} kTableViewCellType;
@interface DetailTableViewController : UITableViewController
{
@private
NSArray *_listArray;
}
@property (nonatomic,assign) UITableViewCellStyle cellType;
@end
@implementation DetailTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_listArray = [UIFont familyNames];
self.tableView.rowHeight = 60 ;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
if (self.cellType == kConcentCellType )
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//第一种定制方法
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 200, 30)];
label.backgroundColor = [UIColor cyanColor];
label.tag = 101;
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];
label.text = _listArray[indexPath.row];
return cell;
} else if (self.cellType == kNibCellType )
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MyView" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
//第二种定制方法
UILabel *label = (UILabel *)[cell.contentView viewWithTag:201];
label.text = _listArray[indexPath.row];
UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:202];
imgView.backgroundColor = indexPath.row %2 == 0 ?[UIColor yellowColor]:[UIColor redColor];
return cell;
}else if (self.cellType == kCustomCellType )
{
//第三种定制方法,单独创建cell类
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//给MyCell类中的text赋值
cell.text = _listArray[indexPath.row];
return cell;
}else{
return nil;
}
}
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell
{
@private
UILabel *_label;
}
@property (nonatomic,copy) NSString *text;
@end
#import "MyCell.h"
//声明私有方法,方便阅读
@interface MyCell()
- (void)initSubViews;
@end
@implementation MyCell
- (void)awakeFromNib {
// Initialization code
}
//重写init方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initSubViews]; //调用方法
}
return self;
}
//定制Cell内容
- (void)initSubViews{
_label = [[UILabel alloc]initWithFrame:CGRectZero];
_label.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:_label];
}
//改变系统样式位置
- (void)layoutSubviews{
[super layoutSubviews]; //一定要调用父类,否则无法排版
//修改label位置
_label.frame = CGRectMake(80, 10, 200, 40);
//给label引用赋值,注意位置
_label.text = self.text;
}