ios - UITableView with fixed section headers -
greets, i'm reading default behaviour of uitableview
pin section header rows top of table scroll through sections until next section pushes previos section row out of view.
i have uitableview
inside uiviewcontroller
, not seem case.
is defualt behaviour uitableviewcontroller
?
here's simplified code based on have. i'll show uicontroller
interface , each table view method i've implemented create table view. have helper data source class helps me index objects use table.
@interface myuiviewcontroller ()<uitableviewdelegate, uitableviewdatasource> @property (nonatomic, readonly) uitableview *mytableview; @property (nonatomic, readonly) mycustomhelperdatasource *helperdatasource; @end //when section data set, details each section , reload table on success - (void)setsectiondata:(nsarray *)sections { super.sectiondata = sections; //this array drives sections //get additional data section details [[restkitservice sharedclient] getsectiondetailsforsection:someid success:^(rkobjectrequestoperation *operation, rkmappingresult *details) { nslog(@"got section details data"); _helperdatasource = [[mycustomhelperdatasource alloc] initwithsections:sections anddetails:details.array]; [mytableview reloaddata]; } failure:^(rkobjectrequestoperation *operation, nserror *error) { nslog(@"failed getting section details"); }]; } #pragma mark <uitableviewdatasource, uitableviewdelegate> - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { if (!_helperdatasource) return 0; return [_helperdatasource countsectionswithdetails]; //number of section have details rows, ignore empty sections } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { //get section object current section int sectionobject *section = [_helperdatasource sectionobjectforsection:section]; //return number of details rows section object @ section return [_helperdatasource countofsectiondetails:section.sectionid]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell * cell; nsstring *cellidentifier = @"sectiondetailcell"; cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier]; cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; cell.textlabel.font = [uifont systemfontofsize:12.0f]; } //get detail object section sectionobject *section = [_helperdatasource sectionobjectforsection:indexpath.section]; nsarray* detailsforsection = [_helperdatasource detailsforsection:section.sectionid] ; sectiondetail *sd = (sectiondetail*)[detailsforsection objectatindex:indexpath.row]; cell.textlabel.text = sd.displaytext; cell.detailtextlabel.text = sd.subtext; cell.detailtextlabel.textcolor = [uicolor bluetextcolor]; return cell; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 50.0f; } - (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section { return 30.0f; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger) section { //get section object current section sectionobject *section = [_helperdatasource sectionobjectforsection:section]; nsstring *title = @"%@ (%d)"; return [nsstring stringwithformat:title, section.name, [_helperdatasource countofsectiondetails:section.sectionid]]; } - (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section { uiview *header = [[uiview alloc] initwithframe:cgrectmake(0, 0, 260, 0)]; header.autoresizingmask = uiviewautoresizingflexiblewidth; header.backgroundcolor = [uicolor darkbackgroundcolor]; sslabel *label = [[sslabel alloc] initwithframe:cgrectmake(3, 3, 260, 24)]; label.font = [uifont boldsystemfontofsize:10.0f]; label.verticaltextalignment = sslabelverticaltextalignmentmiddle; label.backgroundcolor = [uicolor clearcolor]; label.text = [self tableview:tableview titleforheaderinsection:section]; label.textcolor = [uicolor whitecolor]; label.shadowcolor = [uicolor darkgraycolor]; label.shadowoffset = cgsizemake(1.0, 1.0); [header addsubview:label]; return header; }
the headers remain fixed when uitableviewstyle
property of table set uitableviewstyleplain
. if have set uitableviewstylegrouped
, headers scroll cells.
Comments
Post a Comment