ios - Using NSArray to populate collectionView -
i new objective-c, have function accepts nsarray parameter want store array in local var in function , populate collection view through array, ps i'll have update cellforitematindexpath
delegate method in same class.
this method in tableview's cell class. here i've embedded collectionview in cell.
-(void)initcellwithsetsarray:(nsarray *)setsarray{ nsmutablearray *setarray = [nsmutablearray array]; }
this calling method accept array in cellforrowat tableview's delegate method.
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ [cell initcellwithsetsarray :self.setshistoryarray]; }
so need update collectionview based on array parsed in func when called in cellforrowatindexpath
. hope guys picture now.
you need add property cell class hold array:
@property (strong, nonatomic) nsarray *setsarray; -(void) setsetsarray(nsarray *)sets { _setsarray = sets; [self.collectionview reloaddata]; } - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return [self.setsarray count]; }
you can use array in cellforitemat
method in cell class.
also, in cell classes prepareforreuse
should clear existing array , reload collection view:
-(void)prepareforreuse { self.setsarray = nil; [self.collectionview reloaddata]; }
then can assign property in cellforrowat:
:
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ mycellclass *cell = (mycellclass *)[tableview dequeuereusablecellwithidentifier:"mycellidentifier" forindexpath:indexpath]; cell.setsarray = self.setshistoryarray; // note: looks wrong; - should use indexpath.row right data row return cell; }
Comments
Post a Comment