此处给出了实现文件的代码: http://pastie.org/7756763
但是,在上面的示例中,数组是固定的.我有一个sqlite数据库,我存储任务和开始日期等等.我需要创建一个可扩展的视图,其中不同的行是不同的开始日期,当我点击日期时,它应该给出该日期的任务.这是代码:
- (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 * FROM 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 * FROM Todo"]; const char *query_stmt = [selectsql UTF8String]; if (sqlite3_prepare_v2(mDiary,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]; for (int i = 0;i<[allDates count]; i++) { [listofSections addobject:emptyArray]; } self.selectedSection = -1; self.selectedSectionTail = -1;}
表视图方法是:
- (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 is just to check to see if this section is the one we touched UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; if (cell == nil) { cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:CellIDentifIEr]; } NSArray *myArray = [listofSections_DataSource objectAtIndex:indexPath.section];//this Now contains your citIEs Nsstring* myString = [myArray objectAtIndex:indexPath.row]; // get city for that row,under the state cell.textLabel.text = myString; return cell; } else{//THIS IS going to happen the 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 to see if the cell is for exapnding or for selection if([cell.textLabel.text isEqualToString:@"more"]){ // this means we need to expand listofSections = [[NSMutableArray alloc]init]; //setting up the List of section with empty arrays NSMutableArray *emptyArray = [[NSMutableArray alloc]init]; for (int i = 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 the tablevIEw if(selectedSectionTail!=-1) [tasktable reloadSections:(NSIndexSet*)myIndexSet withRowAnimation:UItableVIEwRowAnimationFade]; else { [tasktable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation: UItableVIEwRowAnimationFade]; } //[tasktable reloadData]; } else{ } selectedSectionTail = selectedSection;}
这里的问题是:
1.屏幕上显示的部分数量是正确的,但是当轻敲第一个单元格时,它会消失.
2.当点击任何单元格时,列表不会扩展.
请有人帮帮我吗?谢谢..
解决方法 此示例可能是您要查找的内容: http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html样本来自静态数据,但您应该能够更新它以使用来自数据库的动态数据,并且还可以为日期等提供任务.
总结以上是内存溢出为你收集整理的iOS中的可扩展列表视图全部内容,希望文章能够帮你解决iOS中的可扩展列表视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)