美文网首页iOS杂技
iOS 一个xib中有多个cell使用及关联问题

iOS 一个xib中有多个cell使用及关联问题

作者: 骑马纵天下 | 来源:发表于2019-02-18 10:09 被阅读4次
1. 一个xib文件中创建多个cellcellForRowAtIndexPath方法中查找重用方法。因为xib属于资源文件通过NSBundle来查找,取第几个cell

一个xib中创建多个cell可以直接粘贴复制xib中的cell也可以在控件库中拖拽,记住如果是从控件库中拖拽的cell记得要修改Custom Class,如果不修改关联的属性会无效,只会关联第一个cell如果是从控件库中拖拽的cell红色框内是空白,Xcode10的控件库在右上角,老版的在右下角。

Custom Class 控件库

xib中的cell是有序的按照,从0开始,前提是不修改下图的标识,如果修改需要按照你修改的查找cell

cell

注册cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"UITableViewCell" owner:self options:nil]objectAtIndex:0];
    }
    return cell;
}

或者让创建的cell继承下面的BaseTableCell,就能通过下面的方法查找cell

#import <UIKit/UIKit.h>
@interface BaseTableCell : UITableViewCell
@property (nonatomic, assign) NSInteger cellIndex;
- (id)initWithXibName:(NSString*)strName bundle:(NSBundle *)bundle;
- (id)initWithIndex:(NSInteger)index bundle:(NSBundle *)bundle;
- (CGFloat)calculateHeight;
@end


#import "BaseTableCell.h"
@implementation BaseTableCell
- (id)init{
    return [self initWithIndex:0];
}
- (id)initWithXibName:(NSString*)strName{
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:strName owner:self options:nil];
    self = [nibs objectAtIndex:0];
    if (self) {
        self.cellIndex = 0;
    }
    return self;
}

- (id)initWithIndex:(NSInteger)index{
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    self = [nibs objectAtIndex:index];
    if (self) {
        self.cellIndex = index;
    }
    return self;
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (CGFloat)calculateHeight
{
    self.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.bounds), 0.0f);
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height;
}
@end

使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    if (section == 0) {
        static NSString * cellIdentifier = @"identifier";
        TopUpCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[TopUpCell alloc] initWithIndex:section];
        }
        return cell;
    }
    if (section == 1) {
        static NSString * cellIdentifier = @"identifier1";
        TopUpCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[TopUpCell alloc] initWithIndex:section];
        }
        return cell;
    }
    if (section == 2) {
        static NSString * cellIdentifier = @"identifier2";
        TopUpCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[TopUpCell alloc] initWithIndex:section];
        }
        return cell;
    }
    return nil;
}
2. xib属性关联问题。

新拖进来三个cell上面放好控件添加过约束后,开始拖拽控制为属性,可是每次关联的都是第一个cell,最后检查发现是因为新的几个cell是从控件库中拖拽进去的没有和某个类关联,也就是相当于是一个空的cell跟那个都无关,所以无论怎么拖拽都是关联的第一个cell

command+control+鼠标

关联属性问题:

  1. 点击cell查看属性检查器通过属性检查器和cell某个控件关联,然后在拖拽为属性。
  2. 提前在.h文件中定义好属性,直接和控件关联,如果有多个cell会有选择关联某个cell选项。


    关联

如果有问题可以私信我,或者评论,也有一年多没碰过xib也在学习中。

相关文章

网友评论

    本文标题:iOS 一个xib中有多个cell使用及关联问题

    本文链接:https://www.haomeiwen.com/subject/xlgoeqtx.html