Search code examples
iphonegrouped-table

When empty field comes, removed the row in the Grouped Table view in iPhone?


I have displayed the datas in grouped table view. The data's are displayed in the table view from XML parsing. I have 2 section of the table view, the section one has three rows and section two has two rows.

  section 1 ->  3 Rows

  section 2 - > 2 Rows.

Now i want to check, if anyone of the string is empty then i should remove the empty cells, so i have faced some problems, if i have removed any empty cell, then it will changed the index number. So how can i check, anyone of the field is empty?, Because some times more number of empty field will come, so that the index position will be change. So please send me any sample code or link for that? How can i achieve this?

Sample code,

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section       
 {

 if (section == 0) {

    if([userEmail isEqualToString:@" "] || [phoneNumber isEqualToString:@" "] || [firstName isEqualToString:@" "])
    {
        return 2;

    } 

    else {

        return 3;

    }
}
if (section == 1) {

       if(![gradYear isEqualToString:@" "] || ![graduate isEqualToString:@" "]) {

    return 1;
}
    else
   {
        return 2;
     }


return 0;

}

Please Help me out!!!

Thanks.


Solution

  • As per my understanding, you dont want to add the row where data is empty, so ill suggest you should perpare the sections data before telling the table view about the sections and rows.

    So, may be following code can help you..., i have tested it you just need to call the method "prepareSectionData" from "viewDidLoad" method and define the section arrays in .h file.

    - (void) prepareSectionData {
     NSString *userEmail = @"";
     NSString *phoneNumber = @"";
     NSString *firstName = @"";
    
     NSString *gradYear = @"";
     NSString *graduate = @"";
    
     sectionOneArray = [[NSMutableArray alloc] init];
     [self isEmpty:userEmail]?:[sectionOneArray addObject:userEmail];
     [self isEmpty:phoneNumber]?:[sectionOneArray addObject:phoneNumber];
     [self isEmpty:firstName]?:[sectionOneArray addObject:firstName];
    
     sectionTwoArray = [[NSMutableArray alloc] init];
     [self isEmpty:gradYear]?:[sectionTwoArray addObject:gradYear];
     [self isEmpty:graduate]?:[sectionTwoArray addObject:graduate];
    }
    
     -(BOOL) isEmpty :(NSString*)str{
     if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
         return YES;
     return NO;
     }
    
      // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
    }
    
    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return [sectionOneArray count];
    } else if (section == 1) {
        return [sectionTwoArray count];
    }
    return 0;
    }
    
    
    // Customize the appearance of table view cells.
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell.
    if(indexPath.section == 0){
        cell.textLabel.text = [sectionOneArray objectAtIndex:indexPath.row];
    } else if (indexPath.section == 1) {
        cell.textLabel.text = [sectionTwoArray objectAtIndex:indexPath.row];
    }
    return cell;
    }