首页 > iOS使用xib自定义cell控件为空求大神支招

iOS使用xib自定义cell控件为空求大神支招

这几天在研究autolayout,原先做iOS应用的时候都是纯代码,storyboard和xib很少去碰他,这几天想尝试一下,结果遇到了各种坑。

今天在使用CollectionView的时候,发现用xib拉的控件无法显示,下面是我的步骤。
首先我使用xib进行布局(GoodsCell.xib)

它和GoodsCell.h、GoodsCell.m是关联的,并且我将每个控件都关联到.h文件中了,并且设置了Identifier为goodscell。

接下来,我在viewController里面注册了GoodsCell。 代码如下:


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier]; //注册了nib
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

#pragma mark - collectionView datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return goods.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsCell * cell = (GoodsCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSArray * good = goods[indexPath.row];
    [cell.imageView sd_setImageWithURL:good[0] placeholderImage:[UIImage imageNamed:@"default.png"]];
    [cell.nameView setText:good[1]];
    [cell.priceView setText:[NSString stringWithFormat:@"¥%@",good[2]]];
    cell.backgroundColor = [UIColor blackColor]; //为了看到效果,我将背景设置为黑色
    DLog("%@",cell);
    DLog("imageView%@",cell.imageView);
    DLog("nameView%@",cell.nameView);
    DLog("priceView%@",cell.priceView);
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/2,500); //为了看到效果,我将尺寸设的很大
}

本来以为这样就运行起来就可以了,但是我错了,cell虽然创建了,但是cell里面的imageView等控件却为空的。
效果如下:

可以看到,我再xib中定义的组件都不见了。
下面是我输出的几个对象:

[HomeViewController collectionView:cellForItemAtIndexPath:] [Line 71] <GoodsCell: 0x7fe0b360b7e0; baseClass = UICollectionViewCell; frame = (94 510; 187.5 500); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7fe0b3618c80>>
2015-12-04 15:24:32.118 YuanJing[96009:7164562] -[HomeViewController collectionView:cellForItemAtIndexPath:] [Line 72] imageView(null)
2015-12-04 15:24:32.118 YuanJing[96009:7164562] -[HomeViewController collectionView:cellForItemAtIndexPath:] [Line 73] nameView(null)
2015-12-04 15:24:32.118 YuanJing[96009:7164562] -[HomeViewController collectionView:cellForItemAtIndexPath:] [Line 74] priceView(null)

也就是说cell是创建了,但是子控件都是null,研究了很久还是没发现问题。求各路大神支招。


我是题主。
问题最终还是没有解决。
最终的解决办法是,重新建立的一个项目,使用原来的代码原来的文件运行,结果就行了。
目测是xcode的bug,清理了项目新建也不可以。不知道大家有没有遇到过这种情况。


看不出来,你可以这么尝试下,看看有没有。在xib中给图像视图加上tag:10

UIImageView img = (UIImageView *)[cell viewWithTag:10];
img.backgroundColor = [UIColor redColor];


我也碰到了这个问题, 还没搞定,准备用代码搞了!
后来去stackoverflow找到了答案,具体解决办法是去掉
[self.collectionView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier]; //注册了nib

因为如果在storyboard中设置了ReuseableView的话,就不能再用代码注册重用的cell,具体链接如下:http://stackoverflow.com/ques...

希望新手能找到这个帖子,解决问题!


体住,我遇到了和你同样的问题,最后却是意外解决的,我把Xib的size classes 的勾打上,然后就神奇地出现了,不知道为什么。。。。

【热门文章】
【热门文章】