自定义cell
思路就是创建模型,自定义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的更多相关文章
- 自定义cell自适应高度
UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...
- 自定义cell(xib)中button点击事件不能响应的情况
遇到这种问题真的好尴尬,之前从来没有遇到过,以为手到擒来,未曾料到还会遇到问题! 好多年没有找到尴尬的感觉,现在找到了,真的很尴尬 ! *o* 1.首先使用场景: 原本没打算用xib,后来为了快速, ...
- ios中自定义cell 设置cell的分组结构
ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式 以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...
- iOS开发小技巧--纯代码自定义cell
纯代码自定义cell 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样) 1.新建一个继承自UITableViewCell的子类 2.在initWithStyle:方 ...
- 自定义cell的一些知识
1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...
- 给自定义cell赋值
搭建自定义cell-给自定义cell赋值的思路 1 主控制器 1.1导入头文件 #import "LHQInvestmentManagementCell.h" #import &q ...
- IOS xib在tableview上的简单应用(通过xib自定义cell)
UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib. 这篇随笔介绍的是通过xib ...
- 懒加载 字典转模型 自定义cell
1 懒加载: 1> 什么是懒加载? 懒加载又称为延时加载,即在系统调用的时候加载,如果系统不调用则不会加载.所谓的懒加载其实就是重写其 get 方法. 2> 特点:在使用懒加载的时候要 ...
- iOS深入学习(UITableView系列4:使用xib自定义cell)
可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
随机推荐
- MyBatis知多少(5)业务对象模型
几乎所有结构良好的软件都使用了分层设计.分层设计将一个应用程序根据技术职能分为几 个内聚的部分,从而将某种特定技术或接口的实现细节与其他部分分离开来.分层设计可以用任 何一种强壮的编程语言来实现.图1 ...
- 自己动手搭建 CAS(Central Authentication Service) 环境,为了单点登录(Single Sign On , 简称 SSO )
介绍 刚刚搭建 CAS 成功了,现在记录下来,怕以后忘记,同时也给需要帮助的人.CAS 搭建需要服务端和客户端,服务端是 Java 写的,如果自己搭建则需要安装与配置 Java 环境.客户端可以是多种 ...
- 常用CSS Reset汇总
什么是Css Reset呢? 在 HTML标签在浏览器里有默认的样式,不同浏览器的默认样式之间也会有差别.在切换页面的时候,浏览器的默认样式往往会给我们带来麻烦,影响开发效率.所以解决的方法就是一开始 ...
- DDD:当视图模型、领域模型和数据模型都采用了同样的类型的时候,我们该如何处理?
如果采用这种模式,模型会在不同的逻辑层之间传递,以向内传递为例,模型的状态变化是由外向内的不同逻辑层负责修改的,因为这种模式下模型的封装性是很差的,架构和框架要做到:清晰的表达每个逻辑层该如何使用和修 ...
- C++的最佳特性(译)
最近看到了很多优秀的文章,包括<Why mobile web apps are slow>,实在忍不住翻译出来跟大家分享.这篇文章通过大量的实验和参考文献向我们说明移动应用开发所遇到的问题 ...
- Hadoop入门进阶课程6--MapReduce应用案例
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- [python]非常小的下载图片脚本(非通用)
说在最前面:这不是一个十分通用的下载图片脚本,只是根据我的一个小问题,为了减少我的重复性工作写的脚本. 问题 起因:我的这篇博文什么是真正的程序员浏览量超过了4000+. 问题来了:里面的图片我都是用 ...
- js实现页面a向页面b传参的方法
方法一:使用HTML5本地化存储(localStorage) 组件(本地最大能存储5M数据)localStorage是本地永久存储数据,是cookie的优化 方法二:使用cookie将数据存放在客户的 ...
- 前端引擎初步设计稿 -通过配置生成动态页面 ,LandaSugar平台 .NET-C#-MVC
公司准备开发出一款项目开发平台 LandaSugar,分为 前端引擎.工作引擎.数据引擎 三大块,开发人员只需要对三大模块进行相应的配置便能够完成一个定制项目的开发. 听起来貌似是异想天开,但是是否真 ...
- angular设置title
Javascript框架在处理seo方面存在问题,因为爬虫在检索seo信息的时候会读不了js给其赋的值,导致搜索引擎收录不了或者收录了无效的信息,比如收录的可能是title={{title}}这样的, ...