繁体   English   中英

自定义uicollectionview-传递数据

[英]Custom uicollectionview - pass data

我有一个使用自定义单元格的UICollectionView:

[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellIdentifier"];

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
     cell.articolo = [_array objectAtIndex:indexPath.row];

我想将对象articolo传递到我的单元格,所以我创建了一个属性:

@property (strong, nonatomic) NSMutableDictionary *articolo;

但是如果我尝试使用以下方法之一读取属性:

@implementation CollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {NSLog(@"Articolo: %@",_articolo);}
    return self;
}

- (id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self) {NSLog(@"Articolo: %@",_articolo);}
    return self;
}
- (void) awakeFromNib
{
    [super awakeFromNib];
    NSLog(@"Articolo: %@",_articolo);
}

我总是得到一个空值,错误在哪里? 如何将该对象传递到我的手机中?

试试这个,可能对您有帮助。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
  CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
  cell.articolo =[[NSMutableDictionary alloc] initWithDictionary:[_array objectAtIndex:indexPath.row]];
             return cell;
}

您的代码是完美的,在上述任何一种方法中,您总是会得到null值,因为在初始化单元格时会调用它。

您可以通过放置这样的setter方法来获取价值。

-(void)setArticolo:(NSMutableDictionary *)articolo {

  _articolo = articolo;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM