思路就是创建模型,自定义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. 代码演示用 .NET 4.5 (C# 5.0)自带的压缩类 ZipArchive 创建一个压缩文件

    代码如下: using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; ...

  2. css省略号布局实例截图

    过多文字li标签出现使用css省略号样式截图 使用text-overflow样式让显示不完内容通过css实现省略号排版

  3. [linux笔记]理清linux安装程序用到的(configure, make, make install)

    我作为一名经常和linux打交道的程序员,每次在linux安装软件都祈求可以用——apt-get,yum,brew等应用程序管理器安装,有的时候事与愿违,你只能自己编译安装-wtf,说好的美丽世界呢? ...

  4. Vue基础---->VueJS的使用(一)

    Vue.js是一个构建数据驱动的web界面的库.它的目标是通过尽可能简单的API 实现响应的数据绑定和组合的视图组件,今天我们就开始vue.js的学习. vue的安装及使用 一.vue的下载地址:ht ...

  5. CentOS6.5菜鸟之旅:安装SUN JDK1.7和Tomcat7

    一.前言   CentOS6.5系统自带Open JDK1.7.1.6和1.5,但OpenJDK部分内容与SUN JDK不兼容,因此打算重新安装SUN JDK1.7来开发. 二.卸载Open JDK ...

  6. css命名那些事儿

    根据目前彩票行业的公司进行css命名的层级解析,此次选择了网易彩票,QQ彩票,澳客彩票网,中国竞彩网,500.com,彩票365,新浪彩票,新浪爱彩,凤凰彩票,淘宝彩票的首页进行css命名的采集和分析 ...

  7. knockout.js的简介和简单使用

    1.knockout简介knockout是一个轻量级的UI类库,通过MVVM模式使JavaScript前端UI简单化knockout有四大重要概念:1)声明式绑定:使用简明移读的语法很容易地将模型(m ...

  8. SqlServer一张表数据导入另一张表,收藏使用,工作中更新数据错误很有用

    sql一张表数据导入另一张表   1.如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:   INSERT INTO 目标表 SELECT * FROM 来源表;   2.比如要将 arti ...

  9. javascript类的理解和使用

    距离上次写博客已经过去好几个月了,现在手里的项目正好都结束了,闲暇之后开始理一下开发中一些问题,这次说一下javascript当中的类,可能很多人对于写惯了前台页面效果的coder来说,对于javas ...

  10. csharp: Data binding in WPF DataGrid control

    <Window x:Class="WpfProjectDemo.MainWindow" xmlns="http://schemas.microsoft.com/wi ...