版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaozhuanddapang/article/details/70916174
本章实现效果:
我们使用MVC架构来实现自定义TableView,具体的MVC介绍请自行百度,在本章中不作为主要内容讲解。
Model层:
DataSource.h
#import <Foundation/Foundation.h>
@interface DataSource : NSObject
+ (NSArray *)getUserInfo;
@end
DataSource.m
#import "DataSource.h"
@implementation DataSource
+ (NSArray *)getUserInfo {
NSString * path = [[NSBundle mainBundle] pathForResource:@"users" ofType:@"plist"];
NSArray * arrayReturn = [NSArray arrayWithContentsOfFile:path];
return arrayReturn;
}
@end
View层:
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel * labelName;//姓名
@property (nonatomic, strong) UILabel * labelNum; //手机号
@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
//重写初始化方法:将控件添加到单元格上,如果将子视图控件添加到cell上 借助contenView视图,这样的话cell上子视图会随着cell的变化而变化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.labelName = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.bounds.size.width-200, 40)];
self.labelName.textColor = [UIColor cyanColor];
self.labelName.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.labelName];
self.labelNum = [[UILabel alloc] initWithFrame:CGRectMake(100, 40, self.bounds.size.width-200, 40)];
self.labelNum.textColor = [UIColor greenColor];
self.labelNum.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.labelNum];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
ViewController层:
ViewController.m
#import "ViewController.h"
#import "DataSource.h"
#import "CustomTableViewCell.h"
#import "TwoViewController.h"
@interface ViewController ()<
UITableViewDataSource,
UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *arrayDS;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupDatas];
[self setupSubviews];
}
- (void)setupDatas {
//从Model层拿到数据
NSArray * array = [DataSource getUserInfo];
//把拿到的数据给我们的数据源
self.arrayDS = [[NSMutableArray alloc] initWithArray:array];
self.navigationItem.title = @"自定义单元格";
}
- (void)setupSubviews {
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50)];
view.backgroundColor = [UIColor redColor];
[self.view insertSubview:view aboveSubview:self.tableView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrayDS.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * str = nil;
if ([[[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"phoneNum"] hasPrefix:@"100"]) {
str = @"CustomCell";
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.labelName.text = [[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"personName"];
cell.labelNum.text = [[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"phoneNum"];
return cell;
} else {
str = @"SystemCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = [[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"phoneNum"];
return cell;
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TwoViewController *twoVC = [[TwoViewController alloc] init];
[self.navigationController pushViewController:twoVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
@end