自定义cell

思路就是创建模型,自定义cell,然后在主控制器中完成,首先要观察plist文件:

自定义cell

Contact.h

 #import <Foundation/Foundation.h>

 @interface Contact : NSObject

 @property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *phoneNum;
@property (nonatomic, copy) NSString *imageName; @end

RootCell.h

 #import <UIKit/UIKit.h>

 @interface RootCell : UITableViewCell

 // 联系人头像
@property (nonatomic, strong) UIImageView *headerImageView; // 姓名
@property (nonatomic, strong) UILabel *nameLabel; // 电话号码
@property (nonatomic, strong) UILabel *phoneLabel; @end

RootCell.m

 #import "RootCell.h"

 @implementation RootCell

 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 初始化子视图
[self initLayout];
}
return self;
} // 布局
- (void)initLayout { // 头像
self.headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
//self.headerImageView.backgroundColor = [UIColor orangeColor];
self.headerImageView.layer.cornerRadius = CGRectGetWidth(self.headerImageView.frame) / ;
self.headerImageView.layer.masksToBounds = YES; // cell提供了一个contentView的属性,专门用来自定义cell,防止在cell布局的时候发生布局紊乱,记得将子控件添加到contentView上
[self.contentView addSubview:self.headerImageView]; // 姓名
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headerImageView.frame) + , , , )];
//self.nameLabel.backgroundColor = [UIColor redColor]; [self.contentView addSubview:self.nameLabel]; // 电话号码
self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame) + , , )];
//self.phoneLabel.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:self.phoneLabel];
} @end

RootTableViewController.m

 #import "RootTableViewController.h"
#import "Contact.h"
#import "RootCell.h" @interface RootTableViewController () @property (nonatomic, strong) NSMutableArray *allContactsArray; @end @implementation RootTableViewController // 懒加载
- (NSMutableArray *)allContactsArray { if (_allContactsArray == nil) {
_allContactsArray = [NSMutableArray array];
}
return _allContactsArray;
} - (void)viewDidLoad {
[super viewDidLoad]; // 设置导航栏
self.title = @"通讯录";
self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:]}]; // 读取plist数据
[self handleData]; } // 读取plist数据
- (void)handleData { // 1.获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"Contacts.plist" ofType:nil]; // 2.根据文件路径读取数据
NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
// NSLog(@"%@", dataArray); // 3.将数据转换为model对象
for (NSDictionary *dict in dataArray) { // 3.1 创建model对象
Contact *contact = [[Contact alloc] init]; // 3.2 使用kvc赋值
[contact setValuesForKeysWithDictionary:dict]; // 3.3 把model存放到大数组中
[self.allContactsArray addObject:contact];
}
NSLog(@"%@", self.allContactsArray);
} // 设置分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.allContactsArray.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"rootCell"; // 1.从重用队列里查找可用的cell
RootCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // 2.判断如果没有可重用的cell,就自己创建
if (!cell) {
cell = [[RootCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
} // 3.设置数据
// 取出model对象
Contact *contact = self.allContactsArray[indexPath.section];
// 根据图片名设置头像
cell.headerImageView.image = [UIImage imageNamed:contact.imageName];
cell.nameLabel.text = contact.name;
cell.phoneLabel.text = contact.phoneNum; return cell;
} // 设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return ;
} // 取消屏幕点击效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES];
} @end
上一篇:ios中自定义cell 设置cell的分组结构


下一篇:iOS开发小技巧--纯代码自定义cell