思路就是创建模型,自定义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. 解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:

    php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql extens ...

  2. 2014 网选 5024 Wang Xifeng's Little Plot

    题意:从任意一个任意一个可走的点开始找一个最长的路,这条路如果有转弯的话, 那么必须是 90度,或者没有转弯! 思路: 首先用dfs将所有可走点开始的 8 个方向上的线段的最长长度求出来 ! step ...

  3. 链表的实现(Java语言描述)

    代码如下: public interface ListInterface<T> { public T getElem(int i); public boolean insertElem(i ...

  4. Android 学习笔记之Volley(八)实现网络图片的数据加载

    PS:最后一篇关于Volley框架的博客... 学习内容: 1.使用ImageRequest.java实现网络图片加载 2.使用ImageLoader.java实现网络图片加载 3.使用NetWork ...

  5. .NET 笔试题--自已作答

    以下题目,我已全部作答,答案仅供参考!水平和理解有限,可能有误,欢迎指正,谢谢! 1. 填空: (1)面向对象的语言具有__继承______性._____多态____性.____封装____性. (2 ...

  6. Ubuntu13.04配置:Vim+Syntastic+Vundle+YouCompleteMe

    序言 使用Ubuntu和vim已经有一段时间了,对于Vim下的插件应用,我总是抱着一股狂热的态度.这次,又在网上闲逛着,发现了一个个人博客提到了Vim代码补全这回事,并提到了YouCompleteMe ...

  7. Python 3.x自定义迭代器对象

    Python 3.x与Python 2.x之间存在着较多的语法细节差异.今天在看Python核心编程的时候,说到了自定义迭代器对象.于是动手将源码打了一遍,原书代码如下: class AnyIter( ...

  8. 在Linux上安装Oracle RAC 12 c(12.1) 虚拟机,一步一步向导

    Oracle RAC 12 c(12.1)在Linux上安装虚拟机,一步一步向导 今天我们将看到如何安装 12 c版本1 RAC(真正的应用程序集群)数据库2 Linux 64位的虚拟机 使用VMWa ...

  9. 利用name或id属性设置页面跳转的锚点

    理论准备         网页中的链接按照链接路径的不同,可以分为3种类型,分别是内部类型.锚点链接和外部链接:         按照使用对象的不同,网页中的链接又分为文本超链接,图像超链接,E-ma ...

  10. JS对象的创建与使用

    本文内容:     1.介绍对象的两种类型:     2.创建对象并添加成员:     3.访问对象属性:     4.利用for循环枚举对象的属性类型:     5.利用关键字delete删除对象成 ...