Xib使用之TableViewCell.xib中创建多个Cell

初次使用xib创建UITableviewCell的时候,我都是一个xib文件里,只创建一个Cell,在实际业务中,往往都是一个列表中需要用到多个不同的Cell样式,这就需要创建N个.h .m .xib文件。而且这些.m中的实现还差不多。
后来发现,一个.xib文件中可以创建多个Cell,如图:

多个Cell

这样感觉方便多了。


第一步创建

先和普通创建xibCell一样,在xib中选中左边那个Cell,copy(command + c),然后paste(command + v).xib中就多个Cell了,O(∩_∩)O~~
多个Cell

第二步设置Identifier和代码使用

在代码中创建Cell时

1
2
3
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] firstObject];
}

TempTableViewCell是你的xib文件名,firstObject是第一个Cell,按顺序排的。
第二个怎么办??

1
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:2];

再多依次类推哈。(提示:如果在Cell中添加手势的话,loadNibNamed: 这个返回的数组中会比Cell多哦,大家注意)

设置每个Cell的identifier(identifier 随意起的,我的规律就是类名+第几,不要重复就行),如图:

设置每个Cell的identifier

这样在重用队列中重复使用Cell的时候,能找到正确的Cell,

1
TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TempTableViewCellFirst"];

可以根据indexPath设置不同的identifier。
可以把创建Cell的过程放在Cell.m中,做成类方法,这样不至于VC中的代码过多。
cell.h中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@interface TempTableViewCell : UITableViewCell

/**
* @author god~long, 16-04-03 15:04:19
*
* 初始化Cell的方法
*
* @param tableView 对应的TableView
* @param indexPath 对应的indexPath
*
* @return TempTableViewCell
*/
+ (instancetype)tempTableViewCellWith:(UITableView *)tableView
indexPath:(NSIndexPath *)indexPath;


@end

cell.m中:

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
+ (instancetype)tempTableViewCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"";//对应xib中设置的identifier
NSInteger index = 0; //xib中第几个Cell
switch (indexPath.row) {
case 0:
identifier = @"TempTableViewCellFirst";
index = 0;
break;
case 1:
identifier = @"TempTableViewCellSecond";
index = 1;
break;
case 2:
identifier = @"TempTableViewCellThird";
index = 2;
break;

default:
break;
}
TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:index];
}
return cell;

}

这样VC中:

1
2
3
4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TempTableViewCell *cell = [TempTableViewCell tempTableViewCellWith:tableView indexPath:indexPath];
return cell;
}

是不是很方便呢。

设置属性

快捷设置xib属性:

一. 在.h或者.m中先写好属性,如图:

设置属性

二. 在xib中就可以拖拽连接属性了,如图:

property.gif

设置好属性,下面就是使用了,
配置Cell

1
2
3
4
5
6
7
8
/**
* @author god~long, 16-04-03 16:04:04
*
* 配置TempCell的方法
*
* @param indexPath 对应TableView的indexPath
*/
- (void)configTempCellWith:(NSIndexPath *)indexPath;

.m中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)configTempCellWith:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0: {
_label1.text = @"我是Label1";
_customImageView.image = [UIImage imageNamed:@"8"];
break;
}
case 1: {
_label2.text = @"我是Label2";
[_customButton setTitle:@"我是button" forState:UIControlStateNormal];
break;
}
case 2: {
_label1.text = @"我是第三个Cell的Label1";
_label2.text = @"我是第三个Cell的Label2";
break;
}
default:
break;
}
}

重点:每个Cell设置链接的属性都是单独处理的,没连,在用的时候即使你用了,也设置不了。
运行效果:

运行效果

demo地址:点我哦