ios - Update text in section header (UICollectionReusableView) -
how can update uicollectionview section title? headers (titles) sections in collection view show total number of items available on each section , need update title when user has deleted items collection.
i'm implementing datasource method collectionview:viewforsupplementaryelementofkind:atindexpath: set custom header each 1 of sections in collection view, follows:
- (uicollectionreusableview *)collectionview:(uicollectionview *)collectionview viewforsupplementaryelementofkind:(nsstring *)kind atindexpath:(nsindexpath *)indexpath { uicollectionreusableview *view = nil; if([kind isequaltostring:uicollectionelementkindsectionheader]) { view = [collectionview dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:@"mycustomcollectionheader" forindexpath:indexpath]; mycustomcollectionviewheader *header = (mycustomcollectionviewheader *) view; nsstring *headertitle; if(indexpath.section == 0) { headertitle = [nsstring stringwithformat:@"%lu items", (unsigned long) myarrayofobjectsinfirstsection.count]; } else { headertitle = [nsstring stringwithformat:@"%lu items", (unsigned long) myarrayofobjectsinsecondsection.count]; } header.mylabeltitle.text = headertitle; } homecoming view; } my delete function follows:
- (void)deleteselecteditems { nsarray *indexpaths = self.collectionview.indexpathsforselecteditems; for(nsindexpath *indexpath in indexpaths) { nsstring *numberofitems; if(indexpath.section == 0) { [myarrayofobjectsinfirstsection removeobjectatindex:indexpath.row]; numberofitems = [nsstring stringwithformat:@"%lu items", (unsigned long)myarrayofobjectsinfirstsection.count]; } else { [myarrayofobjectsinsecondsection removeobjectatindex:indexpath.row]; numberofitems = [nsstring stringwithformat:@"%lu items", (unsigned long)myarrayofobjectsinsecondsection.count]; } [self.collectionview deleteitemsatindexpaths:@[indexpath]]; } /* after deleting items, section title must updated new value of numberofitems*/ } my app able set number of items in collection view when app starts, after deleting items collection view header doesn't updated.
please advice
after many multiple attempts came next workaround:
set tag value each section supplementary element... later it's possible search object tag:
- (uicollectionreusableview *)collectionview:(uicollectionview *)collectionview viewforsupplementaryelementofkind:(nsstring *)kind atindexpath:(nsindexpath *)indexpath { uicollectionreusableview *view = nil; if([kind isequaltostring:uicollectionelementkindsectionheader]) { view = [collectionview dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:@"mycustomcollectionheader" forindexpath:indexpath]; mycustomcollectionviewheader *header = (mycustomcollectionviewheader *) view; if(indexpath.section == 0) { header.tag = 100; } else { header.tag = 200; } // ... } homecoming view; } - (void)updatesectiontitles { nsinteger numberofitems; mycollectionviewheader *header; if(indexpath.section == 0) { header = (mycollectionviewheader *) [self.collectionview viewwithtag:100]; numberofitems = myarrayofobjectsinfirstsection.count; } else { header = (mycollectionviewheader *) [self.collectionview viewwithtag:200]; numberofitems = myarrayofobjectsinsecondsection.count; } header.myheadertitlelabel.text = [nsstring stringwithformat:@"there %lu items", (unsigned long) numberofitems]; } } in personal sentiment think shouldn't way go update section supplementary element in uicollectionview.
another alternative phone call reload sections, reloads not supplementary elements (headers, footers) info of each section may performance issue:
[self.collectionview reloadsections:[nsindexset indexsetwithindexesinrange:nsmakerange(0, 2)]]; hope may help in future or post improve approach :)
ios uicollectionview sectionheader uicollectionreusableview
No comments:
Post a Comment