UITableView: Display and hide cells as a dropdown list
对UITableViewController进行更新(增加或删除)过程中可能遇到的问题
.h
interface list : UIViewController { UITableView *mainTableView; NSString *dataForSection0; NSString *dataForSection2; NSMutableArray *demoData; NSMutableArray *demoData1; NSMutableArray *demoData2; int selectedValueIndex; int showingListSectionID; bool isShowingList; }.m
-- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dataForSection0 = @""; //@"This is some cell content."; dataForSection2 = @""; //@"This is another cell content."; demoData = [[NSMutableArray alloc] init]; demoData1 = [[NSMutableArray alloc] init]; [demoData1 addObject:@"student name"]; [demoData1 addObject:@"student1"]; demoData2 = [[NSMutableArray alloc] init]; [demoData2 addObject:@"class name"]; [demoData2 addObject:@"class11"]; [demoData2 addObject:@"class12"]; [demoData2 addObject:@"class13"]; isShowingList = NO; showingListSectionID = -1; selectedValueIndex = 0; //init [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"section0" ]; [[NSUserDefaults standardUserDefaults] synchronize]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"section1" ]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. // The number of the rows is depending on the section index. // The sections with indexes 0 and 2 will have only one row. ///假設你的 numberOfSectionsInTableView 判斷是 return dataSource && [dataSource count] ? 1 : 0 ; 的話,那一開始因為沒資料而 sections 個數為 0,所以更新指定 section 時會出錯,解法有兩個,一個是一開始 numberOfSectionsInTableView 就會回傳固定數值(非0),這樣就不會出錯;另一個則是第一次新增資料時,呼叫 [self.tableView reloadData] 處理,之後才用 insertRowsAtIndexPaths 處理 int rowCount = 1 ; //very important! switch (section) { case 0: if([[NSUserDefaults standardUserDefaults] boolForKey:@"section0"]==YES) rowCount = [demoData1 count]; else rowCount = 1; break; case 1: if([[NSUserDefaults standardUserDefaults] boolForKey:@"section1"]==YES) rowCount = [demoData2 count]; else rowCount = 1; break; default: break; } return rowCount; } // Add header titles in sections. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @""; //@"My first section"; } else if (section == 1){ return @""; //@"My demo section"; } else{ return @""; //@"Another section"; } } // Set the row height. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60.0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... cell.selectionStyle = UITableViewCellSelectionStyleBlue; cell.accessoryType = UITableViewCellAccessoryNone; [[cell textLabel] setFont:[UIFont fontWithName:@"Marker Felt" size:16.0]]; if ([indexPath section] == 0) { demoData = demoData1; } else if ([indexPath section] == 1){ demoData = demoData2; } NSString *showSectionID = [[NSString alloc]initWithFormat:@"section%d",indexPath. section]; // We'll set the dataForSection0 string value to the cell of that section. if ([[NSUserDefaults standardUserDefaults] boolForKey:showSectionID]==NO) { //hide list //[[cell textLabel] setText:[demoData objectAtIndex:selectedValueIndex]]; //only show top one [[cell textLabel] setText:[demoData objectAtIndex:0]]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } else{ //show list // Listing the array items. [[cell textLabel] setText:[demoData objectAtIndex:[indexPath row]]]; if ([indexPath row] == selectedValueIndex) { cell.accessoryType = UITableViewCellAccessoryCheckmark; //check } else{ cell.accessoryType = UITableViewCellAccessoryNone; } } return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { showingListSectionID = indexPath.section; selectedValueIndex = [indexPath row]; NSString *showSectionID = [[NSString alloc]initWithFormat:@"section%d",showingListSectionID]; if([[NSUserDefaults standardUserDefaults] boolForKey:showSectionID]==YES) { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:showSectionID ]; [[NSUserDefaults standardUserDefaults] synchronize]; } else{ [[NSUserDefaults standardUserDefaults] setBool:YES forKey:showSectionID ]; [[NSUserDefaults standardUserDefaults] synchronize]; isShowingList = !isShowingList; } [mainTableView reloadSections:[NSIndexSet indexSetWithIndex:showingListSectionID] withRowAnimation:UITableViewRowAnimationFade]; }
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。