ios - UICollectionView reloadData issue - cell's subview issue -
i have maked following view.
but when calling -reloaddata
, view becomes...
i think it's because cells reused.
have idea keep view first image if calling reloaddata
?
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { customcell* cell = [collectionview dequeuereusablecellwithreuseidentifier:kcellidentifier forindexpath:indexpath]; if (indexpath.item == 0) { cell.label.text = @"hello"; } else { uiimage *image = dataarray[indexpath.item]; cell.imageview.image = image; } return cell; }
customcell.m
@property (nonatomic, strong) uiimageview* imageview; @property (nonatomic, strong) uilabel* label; //... - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { self.imageview = [[uiimageview alloc]initwithframe:imgviewframe]; [self.contentview addsubview:self.imageview]; self.label = [[uilabel alloc]initwithframe:labelframe]; [self.contentview addsubview:self.label]; } return self; }
edit:
i modified code.
- setting default values before setting them correct data
- calling -prepareforreuse
when calling -reloaddata
, although imageview.image
stay nil, label.text
in indexpath.item==0 becomes nil , label.text
in indexpath.item==3 outputs @"hello".
yes, because of reusability. can avoid changing line of code this:
if (indexpath.item == 0) { cell.label.text = @"hello"; cell.imageview.image = nil; } else { uiimage *image = dataarray[indexpath.item]; cell.imageview.image = image; cell.label.text = @""; }
Comments
Post a Comment