iOS高质量博客iosiOS

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

2016-04-03  本文已影响12626人  loongod

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

多个Cell

这样感觉方便多了。


具体实现:

第一步创建

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

多个Cell
第二步设置Identifier和代码使用

在代码中创建Cell

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

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

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

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

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


设置每个Cell的identifier

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

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

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

@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中:

+ (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中:

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

是不是很方便呢。

设置属性

快捷设置xib属性:

1. 在.h或者.m中先写好属性,如图:
设置属性
2. 在xib中就可以拖拽连接属性了,如图:
property.gif

重点:关联属性的时候,你想关联那个Cell上的属性,需要先点击左边Cell列表,选中该Cell,然后再拖线关联上面的控件。
设置好属性,下面就是使用了,
配置Cell

/**
 *  @author god~long, 16-04-03 16:04:04
 *
 *  配置TempCell的方法
 *
 *  @param indexPath 对应TableView的indexPath
 */
- (void)configTempCellWith:(NSIndexPath *)indexPath;

.m中:

- (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设置链接的属性都是单独处理的,没连,在用的时候即使你用了,也设置不了。
运行效果:

运行效果
深入

新建一个UITableViewCell创建的时候选择同时创建xib,然后
右击cell.xib文件➡Open As➡Souce Code 如下图

open source code

然后可以看到xib文件其实是以xml数据格式存储的,如图:

xml

当我改变xib中1和2两个cell的顺序之后(可以双指拖动),然后查看他们的source code会发现,他们在xml文件中的顺序也会改变。用Bundle.main.loadNibNamed("*xibName*", owner: self, options: nil)方法返回数组的Item顺序也会改变。

xib中顺序

我们在调用let cells = Bundle.main.loadNibNamed("*xibName*", owner: self, options: nil)
的时候,加载的是xml<objects/>标签下的数据,其中<placeholder>标签下中如果placeholderIdentifier="IBFilesOwner"或者"IBFirstResponder"时会被忽略。也就是不会返回到上面的cells数组中。但是如果我添加一个手势的话,就会返回到数组中了。
具体如下图:

xib with tapGesture xib with tap souce code cells

其中<objects>中的默认数据顺序是根据我们在xib中添加控件的顺序而定的,下面我又先添加了一个tap手势,然后又添加了一个cell,查看source code和打印cells数据如下图:

中间添加手势 cells log
回答关于重用

我在创建cell的判断里面

    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:index];
        NSLog(@"create cell indexPath: %ld",index);
    }

然后

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

运行并滑动之后截图如下:


界面 控制台

So,是会重用滴。

dome地址:点我点我


上一篇 下一篇

猜你喜欢

热点阅读