objective c - expandable list view in iOS -
i need create expandable list view in ios app, ie. if tap on cell, should expand give more cells. i'm following example given on link: toggle showing/hiding child table cells ios code implementation file given here too: http://pastie.org/7756763
however, in example above arrays fixed. have sqlite database store tasks , start dates, among other things. need create expandable view different rows different start dates , when tap on date, should give tasks date. here's code:
- (void)viewdidload { [super viewdidload]; alldates = [[nsmutablearray alloc]init]; self.date = [[nsmutablearray alloc] initwithobjects: nil]; // self.date = [[nsmutablearray alloc]init]; self.listofsections_datasource = [[nsmutablearray alloc]initwithobjects:nil]; sqlite3_stmt *statement; const char *dbpath = [mdatabasepath utf8string]; if (sqlite3_open(dbpath,&mdiary)== sqlite_ok) { nsstring *selectsql = [nsstring stringwithformat:@"select * todo"]; const char *query_stmt = [selectsql utf8string]; if (sqlite3_prepare_v2(mdiary, query_stmt, -1, &statement, null) == sqlite_ok) { while(sqlite3_step(statement) == sqlite_row) { temptaskname = [[nsstring alloc]initwithutf8string:(const char *) sqlite3_column_text(statement, 2)]; [alldates addobject:[[nsstring alloc]initwithutf8string:(const char *) sqlite3_column_text(statement, 0)]]; [self.date addobject:temptaskname]; } } sqlite3_finalize(statement); } sqlite3_close(mdiary); nslog(@"%i",[alldates count]); for(int x = 0;x < [alldates count];x++) { if (sqlite3_open(dbpath,&mdiary)== sqlite_ok) { nsstring *selectsql = [nsstring stringwithformat:@"select * todo"]; const char *query_stmt = [selectsql utf8string]; if (sqlite3_prepare_v2(mdiary, query_stmt, -1, &statement, null) == sqlite_ok){ temp = [[nsmutablearray alloc]init]; while(sqlite3_step(statement) == sqlite_row) { temptaskname = [[nsstring alloc]initwithutf8string:(const char *) sqlite3_column_text(statement, 2)]; nslog(@"%@",temptaskname); // [alldates addobject:[[nsstring alloc]initwithutf8string:(const char *) sqlite3_column_text(statement, 0)]]; if([[[nsstring alloc]initwithutf8string:(const char *) sqlite3_column_text(statement, 0)] isequaltostring:alldates[x]]) { [self.temp addobject:temptaskname]; } } nslog(@"task name%@",temp); [listofsections_datasource addobject:temp]; } sqlite3_finalize(statement); } sqlite3_close(mdiary); } //[self.listofsections_datasource addobject:alldates]; nsmutablearray *emptyarray = [[nsmutablearray alloc]init]; listofsections = [[nsmutablearray alloc]init]; (int = 0;i<[alldates count]; i++) { [listofsections addobject:emptyarray]; } self.selectedsection = -1; self.selectedsectiontail = -1; } the table view methods are:
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [listofsections_datasource count]; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if(selectedsection !=section) return 1; else{ nsarray *array = [listofsections objectatindex:section]; return [array count]; } } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; if(self.selectedsection == indexpath.section){//this check see if section 1 touched uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } nsarray *myarray = [listofsections_datasource objectatindex:indexpath.section];//this contains cities nsstring* mystring = [myarray objectatindex:indexpath.row]; // city row, under state cell.textlabel.text = mystring; return cell; } else{//this going happen first time uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } cell.textlabel.text = @"more"; return cell; } } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell * cell = [tableview cellforrowatindexpath:indexpath]; //check see if cell exapnding or selection if([cell.textlabel.text isequaltostring:@"more"]){ // means need expand listofsections = [[nsmutablearray alloc]init]; //setting list of section empty arrays nsmutablearray *emptyarray = [[nsmutablearray alloc]init]; (int = 0;i<[alldates count]; i++) { [listofsections addobject:emptyarray]; } //add array of tasks here [listofsections replaceobjectatindex:indexpath.section withobject:[listofsections_datasource objectatindex:indexpath.section]]; int tapedrow = [indexpath section]; self.selectedsection = tapedrow; nsmutableindexset *myindexset = [[nsmutableindexset alloc]init]; [myindexset addindex:selectedsection]; [myindexset addindex:selectedsectiontail]; // updating section in tableview if(selectedsectiontail!=-1) [tasktable reloadsections:(nsindexset*)myindexset withrowanimation:uitableviewrowanimationfade]; else { [tasktable reloadsections:[nsindexset indexsetwithindex:indexpath.section] withrowanimation: uitableviewrowanimationfade]; } //[tasktable reloaddata]; } else{ } selectedsectiontail = selectedsection; } the problem here that: 1. no. of sections shows on screen correct when first cell tapped, disappears. 2. list doesn't expanded when of cells tapped.
please can me out? thanks..
this sample may you're looking for: http://developer.apple.com/library/ios/#samplecode/tableviewupdates/introduction/intro.html
the sample pulls static data, should able update use dynamic data db, , give tasks date, etc.
Comments
Post a Comment