iOS深入学习(UITableView系列4:使用xib自定义cell)

2024-03-10 18:30

本文主要是介绍iOS深入学习(UITableView系列4:使用xib自定义cell),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//CustomTableViewCell.h文件
@interface CustomTableViewCell:UITableViewCell
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *textLabel;
@end
//CustomTableViewCell.m文件
@implementation CustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if  (self) {
         // Initialization code
         _headImageView = [[UIImageView alloc] init];
         [self.contentView addSubView:_headImageView];
         _nameLabel = [[UILabel alloc] init];
         /*省略Label的属性设置*/
         [self.contentView addSubView:_nameLabel];
         _textLabel = [[UILable alloc] init];
         /*省略Label的属性设置*/
         [self.contentView addSubView:_textLabel];
     }
     return  self;
}
- ( void )layoutSubviews
{
     //省略布局代码
     self.headImageView.frame = CGRectMake(.....);
     self.nameLabel.frame = CGRectMake(.....);
     self.textLabel.frame = CGRectMake(....);
     [super layoutSubviews];
}
@end

上面CustomTableViewCell的.h/.m文件中,我用了大段的代码来给自定义的cell布局,真的挺麻烦。然后在ViewController中使用的时候是这样的,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static  NSString *simpleIdentify = @ "SimpleIdentify" ;
     CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];
     if (cell == nil)
     {
         cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentify];
     }
     //_person就是UITableView的数据源,它里面存放的是Person数据模型,
     //Person包括头像headStr、name、speechText等属性
     Person *person = [_persons objectAtIndex:indexPath.row];
     cell.headImage.image = [UIImage imageNamed:person.headStr];
     cell.nameLabel.text = person.name;
     cell.textLabel.text = person.speechText;
     return  cell;
}

上面就是使用代码自定义cell所要做的工作,我们当cell显示没有达到预期的时候我们还要回头改变其中UI控件的坐标,或者更改控件属性,当然这只是要多花点时间,最终还是可以实现的。

但是。。。我们还是要试着提高自己的开发效率,使用越来越成熟的xib技术,可以很大地提高开发效率,当然也是要勤加练习,熟能生巧。

开始阅读下面的内容之前,我假设你已经看过我之前的博客-UITableView系列1,那篇博客是本篇博客的基础;如果你没有看过,那就看看吧,然后接着看本篇博客。

所以下面开始学习吧!Come on!

按照下面的步骤开始操作,

(1)新建一个空的(Empty)xib文件,File->New,在面板中选择User Interface->Empty,如下图,

将文件命名为CustomTableViewCell,表示自定义的cell意思。

(2)拖动一个TableViewCell到空的(Empty)xib文件中,如下图,

(3)修改CustomTableViewCell的高度为90,通过属性面板来设置,如下图,

(4)拖一个UIIMageView到CustomTableViewCell的xib文件,设置该UIImageView控件的tag值为10;再拖两个UILabel到xib文件,设置tag分别为1和2,其布局方式如下图,

上面的几个步骤就进行了CustomTableViewCell的自定义,下面的步骤就是使用这个通过xib文件进行自定义的cell。

(5)选中CustomTableViewCell,点击左上角的File's Owner,如下图,

选中这个xib文件中的File's Owner是为了设置CustomTableViewCell的文件所有者,否则无法与.h/.m文件连线,接着看下一步。

(6)在右侧的面板,选择“Show the Identify Inpector”选项,如下图

这时候我们发现CustomTableViewCell的File's Owner为NSObject,因为我要在我的RootViewController中使用,所以我将"NSObject"替换为"RootViewController",表明这个cell的所有者是RootViewController。(PS:你需要将此处的NSObject改为你使用该CustomTableViewCell的ViewController文件名。)

(7)在使用该CustomTableViewCell的ViewController的头文件中写下如下代码,

?
1
2
3
4
5
6
@interface RootViewController:UIViewController
{
}
@property (nonatomic, strong) IBOutlet UITableViewCell *customCell;
@end

有人会奇怪这段话是什么意思,接着下面的步骤,你就会明白。

(8)在CustomTableViewCell.xib文件中,拖动File's Owner指向TableViewCell,如下图,

(9)在弹出的界面中选择customCell选项,如下图,

这就是为什么第(7)步骤要在RootViewController.h文件中声明一个IBOutlet关键字修饰的customCell变量的原因了。

(10)新建一个数据模型文件Person,继承自NSObject,其代码如下,

?
1
2
3
4
5
6
7
8
9
10
11
12
//Person.h文件
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *headStr;
@property (nonatomic, strong) NSString *speechText;
@end
//Person.m文件
#import "Person.h"
@implementation Person
@end

之所以叫它模型文件,是因为该文件中的属性与CustomTableViewCell上面的控件所需的内容一致,以MVC的视角来看Person就是M(Model),CustomTableViewCell就是V(View),而RootViewController就是C(Controller)。

(11)在RootViewController中初始化TableView数据源_persons,代码如下,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>
{
     NSArray *_persons;
}
@end
@implementation RootViewController
- ( void )viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view from its nib.
     Person *p0 = [[Person alloc] init];
     p0.name = @ "路飞" ;
     p0.headStr = @ "person0" ;
     p0.speechText = @ "我要当海贼王!" ;
     Person *p1 = [[Person alloc] init];
     p1.name = @ "卓洛" ;
     p1.headStr = @ "person1" ;
     p1.speechText = @ "受尽磨难而不折,此乃修罗之道!" ;
     
     Person *p2 = [[Person alloc] init];
     p2.name = @ "罗宾" ;
     p2.headStr = @ "person2" ;
     p2.speechText = @ "我要活下去,把我带向大海吧!" ;
     
     Person *p3 = [[Person alloc] init];
     p3.name = @ "娜美" ;
     p3.headStr = @ "person3" ;
     p3.speechText = @ "帮帮我,路飞!" ;
     
     _persons = [NSArray arrayWithObjects:p0,p1,p2,p3, nil];
}
@end

因为我是海贼迷,所以找了路飞、卓洛、娜美、罗宾的图片当做头像,你可以到网上down几张,改名为person0.png、person1.png、person2.png、person3.png。

(12)在-tableView:cellForRowAtIndexPath:中使用CustomTableViewCell.xib创建cell,代码如下,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma mark - UITableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return  [_persons count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static  NSString *simpleIdentify = @ "SimpleIdentify" ;
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@ "CustomTableViewCell"  owner:self options:nil];
     if  ([nib count]>0)
     {
         self.customCell = [nib objectAtIndex:0];
         cell = self.customCell;
     }
     //获取数据源中_person数组中的元素,对应每一个cell
     Person *person = [_persons objectAtIndex:indexPath.row];
     //通过tag值来获取UIImageView和UILabel
     UIImageView *headImageView = (UIImageView *)[cell.contentView viewWithTag:0];
     UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
     UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:2];
     headImageView.image = [UIImage imageNamed:person.headStr];
     nameLabel.text = person.name;
     textLabel.text = person.speechText;
     
     return  cell;
}

最终的效果图如下,

总结:上面的步骤看起来复杂,但是当你习惯xib来编程的时候,使用鼠标拖拉空间,微调界面,那种感觉就像自己不是纯粹的程序员,还是一个设计师。

我把源代码放到了Github上面,有兴趣的朋友可以去下载看看。或者在Mac终端,输入git clone https://github.com/pythonhater/TableViewSamples.git,即可获得源代码。

这篇关于iOS深入学习(UITableView系列4:使用xib自定义cell)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/795126

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定