新手教程之使用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实现一个简单的微博界面布 ...
随机推荐
- oracle不用tsname文件的时候着怎么办
oracle\product\10.2.0\client_2\odp.net\PublisherPolicy\Policy.9.2.Oracle.DataAccess.config 找到newVers ...
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
- HDU 5828 Rikka with Sequence (线段树+剪枝优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...
- Java中的BASE64
located in rt.jar... public class sun.misc.BASE64Encoder extends sun.misc.CharacterEncoder{ //.. } p ...
- oracle 学习笔记--用户管理
oracle 用户管理 创建用户(需要具有dba权限的用户) create user 用户名 identified by 密码 defaule tablespace users //默认表空间 ...
- 重学HTML
http://www.imooc.com/learn/9 1.em/strong 如果想在一段话中特别强调某几个文字,这时候就可以用到<em>或<strong>标签. 但两者在 ...
- CriminalIntent程序中Fragment相关内容
Activity中托管UI fragment有两种方式: 添加fragment到acitivity中 在activity代码中添加fragment 第一种方法即将fragment添加到acitivit ...
- xhtml和css概述
Xhtml和css概述 1.html的过渡到xhtml html与xhtml不是两种语言,它们是一种语言的不同阶段,有点类似于文言文和白话文之间的关系.因为网络技术的日新月异,html的不断改进,所以 ...
- Ucenter后台登陆 验证码CCCC的解决方法 无法登录解决办法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- FLEX4中的Panel如何实现带自定义图标和按钮
做过flex开发的程序员都知道,使用flex3中的panel自定义按钮很容易,而且flex3的panel有icon属性.但是flex4的中大部分的控件与flex3中的控件实现方式有很大的变化,同是 ...