新手教程之使用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的更多相关文章

  1. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  2. 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...

  3. 通过xib自定义UITableViewCell

    通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...

  4. 用xib自定义UITableViewCell的注意事项——重用

    问题的提出: 有时候我们经常需要自定义tableView的cell,当cell里面的布局较为复杂时往往舍弃纯代码的方式而改用xib的方式进行自定义.当我们用纯代码的方式布局cell时,往往会在cell ...

  5. 【swift学习笔记】三.使用xib自定义UITableViewCell

    使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...

  6. 用xib自定义UITableViewCell

    1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 #import "Mo ...

  7. IOS学习之路七(通过xib自定义UITableViewCell)

    一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...

  8. 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

    自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...

  9. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

随机推荐

  1. SQL Script 杂记

    1.提交sql server中未提交的事务 commit select   @@TRANCOUNT 2.查询存储过程中包含某个字符串的所有存储过程 SELECT *FROM   INFORMATION ...

  2. 结构类模式(二):桥接(Bridge)

    定义 将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化. 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维 ...

  3. This is a sandbox of markdown

    A First Level Header A Second Level Header Now is the time for all good men to come to the aid of th ...

  4. Android 模仿电视机关闭界面

    Tween动画 1.在关闭电视机的时候,电视机中间都有一根白条瞬间关闭. 要实现这个效果其实就是利用Tween动画进行实现的. 动画的xml 文件是: android:startOffset=&quo ...

  5. ActiveMQ讯息传送机制以及ACK机制详解

    [http://www.ylzx8.cn/ruanjiangongcheng/software-architecture-design/11922.html] AcitveMQ:消息存储和分发组件,涉 ...

  6. gulp安装和使用简介

    一. gulp和grunt对比 grunt目前的工作流程:读文件.修改文件.写文件——读文件.修改文件.写文件——... gulp目前的工作流程:读取文件——修改文件——修改文件...——写文件 二. ...

  7. WinSock异步IO模型之Select

    如果你想在Windows平台上构建服务器应用,那么I/O模型是你必须考虑的. Windows操作系统提供了五种I/O模型,分别是: ■ 选择(select): ■ 异步选择(WSAAsyncSelec ...

  8. PL/pgSQL学习笔记之三

    http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1.2. Supported Argument and Resul ...

  9. BZOJ 1083: [SCOI2005]繁忙的都市 kruskal

    1083: [SCOI2005]繁忙的都市 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 Description 城市C是一个非 ...

  10. Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律

    Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...