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

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

自定义cell的更多相关文章

  1. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  2. 自定义cell(xib)中button点击事件不能响应的情况

    遇到这种问题真的好尴尬,之前从来没有遇到过,以为手到擒来,未曾料到还会遇到问题! 好多年没有找到尴尬的感觉,现在找到了,真的很尴尬 !  *o* 1.首先使用场景: 原本没打算用xib,后来为了快速, ...

  3. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  4. iOS开发小技巧--纯代码自定义cell

    纯代码自定义cell 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样) 1.新建一个继承自UITableViewCell的子类 2.在initWithStyle:方 ...

  5. 自定义cell的一些知识

    1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...

  6. 给自定义cell赋值

    搭建自定义cell-给自定义cell赋值的思路 1 主控制器 1.1导入头文件 #import "LHQInvestmentManagementCell.h" #import &q ...

  7. IOS xib在tableview上的简单应用(通过xib自定义cell)

    UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib. 这篇随笔介绍的是通过xib ...

  8. 懒加载 字典转模型 自定义cell

    1 懒加载: 1>  什么是懒加载? 懒加载又称为延时加载,即在系统调用的时候加载,如果系统不调用则不会加载.所谓的懒加载其实就是重写其 get 方法. 2>  特点:在使用懒加载的时候要 ...

  9. iOS深入学习(UITableView系列4:使用xib自定义cell)

    可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

随机推荐

  1. B2B多商铺初期权限数据库设计

    项目从无到有,两个月了.一期完成. 权限目前还很简单.USER表,ROLE表,RESOURCE表三个. 目前只有两个商铺.id是0的是我们自己,作为后台运维管理,也抽象成一个商铺,id为0.另一个商铺 ...

  2. java io系列06之 序列化总结(Serializable 和 Externalizable)

    本章,我们对序列化进行深入的学习和探讨.学习内容,包括序列化的作用.用途.用法,以及对实现序列化的2种方式Serializable和Externalizable的深入研究. 转载请注明出处:http: ...

  3. Direct3D11学习:(二)基本绘图概念和基本类型

    转载请注明出处:http://www.cnblogs.com/Ray1024   一.概述 在正式开始学习D3D11之前,我们必需首先学习必要的基础知识. 在这篇文章中,我们将介绍一下Direct3D ...

  4. awk分隔符设定为多个字符或字符串

    awk -F"[01]" '{}'  这种形式指定的分隔符是或的关系,即0或1作为分隔符:awk -F"[0][1]" '{}' 这种形式指定的分隔符是合并的关 ...

  5. 如何转移数据库MDF和LDF文件

    我们可以很轻易地使用SQL Server来创建一个数据库,创建的数据库实例将存储在指定的默认位置(不一定是C盘,可以手动变更默认存储位置).假设此时数据库实例创建在了C盘中的默认位置,亦即是与数据库安 ...

  6. C#多态--虚方法实现多态

    1.虚方法提供一种默认实现,子类可以选择是否重写,如果不重写,那么就使用父类已经实现的方法.(重写可以改变方法的指针) 如果需要改变类型指针,那么需要做方法的重写: 1.如果子类方法是重写方法,那么系 ...

  7. C#设计模式——职责链模式(Chain Of Responsibility Pattern)

    一.概述 在软件开发中,某一个对象的请求可能会被多个对象处理,但每次最多只有一个对象处理该请求,对这类问题如果显示指定请求的处理对象,那么势必会造成请求与处理的紧耦合,为了将请求与处理解耦,我们可以使 ...

  8. 给文本框添加模糊搜索功能(“我记录”MVC框架下实现)

    步骤: 1.在文本框中输入内容时,触发keyup事件: 2.在keyup事件的处理方法中,通过Ajax调用控制器的方法: 3.在控制器方法中,搜索满足条件的数据,这里分页获取数据,且只取第一页的数据, ...

  9. 在C#后端处理一些结果然传给前端Javascript或是jQuery

    在C#后端处理一些结果然传给前端Javascript或是jQuery,以前Insus.NET有做过一个例子<把CS值传给JS使用 >http://www.cnblogs.com/insus ...

  10. 关于SVN删除后的文件不能重新添加(正常途径不行)

    在你自己的机器上(即SVN客户端),把“新建test”文件夹标记为删除,然后提交,在删除之前可以备份“新建test”文件夹,提交后,在当前文件夹下更新SVN.然后把你刚刚备份的文件夹重新放到该目录下. ...