We help IT Professionals succeed at work.

NumberOfRowsInSection problem

1,142 Views
Last Modified: 2012-02-11
Hi, I have an iphone App that reads the "category" field from the xml file that is a rss feed. The way my App works is that it displays the content of the rss feed in a table view by categories from the xml "category" field.

Im a bit new to tableviews so im a bit lost.

I just have 2 categories on the xml file, one called "Uncategorized" and another called "Promos".

The current code is:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
		case 0:
         
            return itemsToDisplay.count;
		default:
   
            return itemsToDisplay.count;
	}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if(section == 0)
        return @"Promoções";
    else
        return @"Não Categorizados";
}
// 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
	// Configure the cell.
  
                
                if (indexPath.section == 0) {
                    MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
                    if([item.category isEqualToString:@"PROMOS"]){
                        MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
                        NSLog(@"ENTRA NO PROMOS______________________________________________________");
                        NSLog(@"item.category = %@--------------------->", item.category);
                    // Process
                        NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
                        NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]";
                        NSLog(@"IMAGE (table View) = %@",item.image);
                   
                        NSURL *url = [NSURL URLWithString:item.image];
                        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
                        [request setDelegate:self];
                        [request startAsynchronous];
                        NSData * imageData = [NSData dataWithContentsOfURL:url];
                        UIImage * image = [UIImage imageWithData:imageData];
                    // Set
                        cell.imageView.image = image;
                        cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
                        cell.textLabel.text = itemTitle;
                        NSMutableString *subtitle = [NSMutableString string];
                        if (item.date) [subtitle appendFormat:@"%@: ", [formatter stringFromDate:item.date]];
                        [subtitle appendString:itemSummary];
                        cell.detailTextLabel.text = subtitle;
                        NSLog(@"FIM DO PROMOS________________________________________________________");
                    }
                }else if(indexPath.section == 1){
                    MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
                    if([item.category isEqualToString:@"Uncategorized"]){
                        NSLog(@"ENTRA NO UNCATEGORIZED_______________________________________________");
                        NSLog(@"item.category = %@--------------------->", item.category);
                    // Process
                        NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
                        NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]";
                        NSLog(@"IMAGE (table View) = %@",item.image);
                    
                        NSURL *url = [NSURL URLWithString:item.image];
                        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
                        [request setDelegate:self];
                        [request startAsynchronous];
                        NSData * imageData = [NSData dataWithContentsOfURL:url];
                        UIImage * image = [UIImage imageWithData:imageData];
                    // Set
                        cell.imageView.image = image;
                        cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
                        cell.textLabel.text = itemTitle;
                        NSMutableString *subtitle = [NSMutableString string];
                        if (item.date) [subtitle appendFormat:@"%@: ", [formatter stringFromDate:item.date]];
                        [subtitle appendString:itemSummary];
                        cell.detailTextLabel.text = subtitle;
                        NSLog(@"FIM DO UNCATEGORIZED____________________________________________________");
                }
            
		
	}
    return cell;
}

Open in new window


The problem i have is that it displays the same number of cells for both categories and doesnt filter them by Categorie.

Best Regards.
Comment
Watch Question

Hamidreza VakilianSenior iOS/Android Developer
CERTIFIED EXPERT

Commented:
Change numberOfRowsInSection method so that it returns number of rows of first category when section==0 and number of rows of second category when section==1.

Sample:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
		case 0:
                           return 10;//number of rows in first category
		case 1:
                           return 30;//number of rows in second category
	}
}

Open in new window

Author

Commented:
Thanks for the answer but that didn't solve my problem :)

The number of rows in section is determined by the  amount of objects parsed by the xml that are going to be inserted on the given category.
Using fixed values gets me an error.
Hamidreza VakilianSenior iOS/Android Developer
CERTIFIED EXPERT

Commented:
That was just a sample. When you parse the elements you can calculate number of rows in each category so store them into 2 separate variables then pass them in appropriate categories in numberOfRowsInSection method.
Senior iOS/Android Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Awesome :)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.