新手教程之使用Xib自定义UITableViewCell
新手教程之使用Xib自定义UITableViewCell
前言
首先:什么是UITableView?看图

其次:什么是cell?

然后:为什么要自定cell,UITableView不是自带的有cell么?
因为在日常开发中,系统自带的cell满足不了客户和开发人员的需求(并且每个cell中的内容\大小\样式相同),我们就需要自定义cell来实现更加优化的功能.比如下面这种

最后:怎么自定义cell?
1.创建一个新的项目,在storyboard中拖入两个imageView,两个label

2.在ViewController里面创建UITableView
//
// ViewController.m
// Xib自定义UITableViewCell
//
// Created by admin on 16/5/16.
// Copyright © 2016年 KXZDJ. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self config];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} -(void)config {
//初始化tableView,并给tableView设置frame以及样式
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
//遵守代理和数据源(因为要用到代理和数据源方法)
self.tableView.delegate = self;
self.tableView.dataSource = self;
//添加到ViewController的视图中
[self.view addSubview:self.tableView];
} /**
* 返回多少个组(默认是1组,如果只有一组可以不实现这个方法)
*
* @param tableView 当前tableView
*
* @return 组的个数
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
}
/**
* 每一组返回多少行
*
* @param tableView 当前tableView
* @param section 当前组
*
* @return 行的个数
*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//指定cell的重用标识符
static NSString *reuseIdentifier = @"CELL";
//去缓存池找名叫reuseIdentifier的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//如果缓存池中没有,那么创建一个新的cell
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
//返回当前cell
return cell;
}
系统自带的UITableView
运行效果

4.Xib自定义cell
首先我们要创建一个xib文件,有两种创建方式:
直接上图
第一种:


第二种:



第二种在创建类的时候也同时创建了xib,比较方便,要是用第一种方式创建,还得关联类

下面就要拖控件到Xib的cell中并给控件设置布局(约束)了


下面进入代码阶段
5.获取Xib自定义的cell
代码:(XibTableViewCell.h)
//
// XibTableViewCell.h
// Xib自定义UITableViewCell
//
// Created by admin on 16/5/16.
// Copyright © 2016年 KXZDJ. All rights reserved.
// #import <UIKit/UIKit.h> @interface XibTableViewCell : UITableViewCell
//加载xib的方法(自己写的,不是系统自带)
+(instancetype)xibTableViewCell; @end
(XibTableViewCell.m)
//
// XibTableViewCell.m
// Xib自定义UITableViewCell
//
// Created by admin on 16/5/16.
// Copyright © 2016年 KXZDJ. All rights reserved.
// #import "XibTableViewCell.h" @implementation XibTableViewCell
//实现类方法
+(instancetype)xibTableViewCell {
//在类方法中加载xib文件,注意:loadNibNamed:owner:options:这个方法返回的是NSArray,所以在后面加上firstObject或者lastObject或者[0]都可以;因为我们的Xib文件中,只有一个cell
return [[[NSBundle mainBundle] loadNibNamed:@"XibTableViewCell" owner:nil options:nil] lastObject];
} - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end
6.把xib文件加载到系统的UITableView中替换系统自带的cell
这一步必须在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中进行,或者是封装在自己定义的类中,不过就算封装了,也要在这个方法中调用.
6.1给控件拖线关联到类中,方便调用控件

6.2代码:
//
// ViewController.m
// Xib自定义UITableViewCell
//
// Created by admin on 16/5/16.
// Copyright © 2016年 KXZDJ. All rights reserved.
// #import "ViewController.h"
//导入头文件
#import "XibTableViewCell.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self config];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} -(void)config {
//初始化tableView,并给tableView设置frame以及样式
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
//遵守代理和数据源(因为要用到代理和数据源方法)
self.tableView.delegate = self;
self.tableView.dataSource = self;
//添加到ViewController的视图中
[self.view addSubview:self.tableView];
} /**
* 返回多少个组(默认是1组,如果只有一组可以不实现这个方法)
*
* @param tableView 当前tableView
*
* @return 组的个数
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
}
/**
* 每一组返回多少行
*
* @param tableView 当前tableView
* @param section 当前组
*
* @return 行的个数
*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//指定cell的重用标识符
static NSString *reuseIdentifier = @"CELL";
//去缓存池找名叫reuseIdentifier的cell
//这里换成自己定义的cell
XibTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//如果缓存池中没有,那么创建一个新的cell
if (!cell) {
//这里换成自己定义的cell,并调用类方法加载xib文件
cell = [XibTableViewCell xibTableViewCell];
}
//给cell赋值
cell.backView.image = [UIImage imageNamed:@"223733vuf3mhajhd04hdh5"];
cell.infoLabel.text = @"金三胖真帅";
cell.infoLabel.textColor = [UIColor redColor];
cell.zanView.image = [UIImage imageNamed:@"103112778vn00czp59p6w7"];
cell.zanLabel.text = @"";
cell.zanLabel.textColor = [UIColor redColor];
//返回当前cell
return cell;
}
/**
* 返回cell的行高
*
* @param tableView 当前tableView
* @param indexPath
*
* @return cell的行高
*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ;
} @end
运行效果

总结:到此Xib自定义UITableViewCell就告一段落,如有错误,烦请各位指正,希望能帮到大家,不是故意黑金三胖.
新手教程之使用Xib自定义UITableViewCell的更多相关文章
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- 【转】iOS 通过xib自定义UITableViewCell【原创】
原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...
- 通过xib自定义UITableViewCell
通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...
- 用xib自定义UITableViewCell的注意事项——重用
问题的提出: 有时候我们经常需要自定义tableView的cell,当cell里面的布局较为复杂时往往舍弃纯代码的方式而改用xib的方式进行自定义.当我们用纯代码的方式布局cell时,往往会在cell ...
- 【swift学习笔记】三.使用xib自定义UITableViewCell
使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...
- 用xib自定义UITableViewCell
1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 #import "Mo ...
- IOS学习之路七(通过xib自定义UITableViewCell)
一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...
- 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
随机推荐
- thinkPHP 无法create,无法插入数据,提示非法数据对象
4.thinkPHP 无法create,提示非法数据对象解决方法:不要create+add,而用 data[]= '';+add$m_r_fa_account = D('R_fa_account'); ...
- 解决sencha touch显示.JSON包含中文数据时显示乱码问题
按照ST官方示例navigationview做的一个示例.数据源是一个.json文件.但是显示的时候如果.json文件里有中文则乱码.我知道是编码问题,但是不知道怎么改,如何改. 问了N个人最后解决方 ...
- Linux下如何用vi编辑和保存文件
vi是Linux终端下或控制台下常用的编辑器,基本的操作方式为:vi /路径/文件名 例如,vi /etc/fstab表示显示/etc/fstab文件的内容.使用键盘上的Page Up和Page Do ...
- Spring REST实践之Versioning,Paging和Sorting
Versioning 为适应需求的变化以及兼容已有的API,需要创建新版本的API,一般有四种流行的版本化API的方法: URI版本化 URI参数版本化 Accept header版本化 自定义hea ...
- .net基础知识
1.private.protected.public.internal修饰符的访问权限 Private:私有成员,只能在当前类中才可以访问 Protected:保护成员,在当前类和继承类中访问 ...
- Regular Expression--Good parts
匹配URL的正则表达式 <!doctype html><html lang="en"><head> <meta charset=" ...
- xshell linux传文件
yum install lrzsz 安装完毕即可使用 rz,sz是便是Linux/Unix同Windows进行ZModem文件传输的命令行工具 windows端需要支持ZModem的telnet/s ...
- php 扩展 redis
1.通过phpinfo 查看php的版本( 要注意php 是nts 还是ts 通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabl ...
- 记录一些在VPS上折腾的东西
折腾这些东西,总是要经常借助搜索引擎找答案,找的次数多了,也就烦了,不想总是做重复工作. 所以把做过的一些事情记录一下,加深一下印象. 1.安装python2.7 VPS上面的太老了,之前安装的,过程 ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...