0

Here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRowsPerSection = 0;

    if (section == 0) {
        for (int i = 0; i < [[[BNRItemStore sharedStore] allItems] count]; i ++) {
            BNRItem *item = [[[BNRItemStore sharedStore] allItems] objectAtIndex:i];
            if ([item valueInDollars] > 50) {
                numberOfRowsPerSection ++;
            }
        }
    }else{
        for (int i = 0; i < [[[BNRItemStore sharedStore] allItems] count]; i ++) {
            BNRItem *item = [[[BNRItemStore sharedStore] allItems] objectAtIndex:i];
            if ([item valueInDollars] == 73) {
                numberOfRowsPerSection ++;
            }
        }
    }

    return numberOfRowsPerSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    if (!cell) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }

    BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
    if ([p valueInDollars] > 50 && indexPath.section == 0) {
        [[cell textLabel] setText:[p description]];
    }else if(indexPath.section == 1){
        [[cell textLabel] setText:[p description]];
    }


    return cell;
}

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

I want to display in one section results > 50 and the other section the rest of the result but I don't know how to do it. I am getting duplicate results in each section.

Thanks

2
  • What is your control mechanism? You first check to see if greater than 50 and on the other section, you only check if it equals to 73. What does it exactly mean? This doesn't seem like results greater than 50 and the 'rest'. Commented Jul 6, 2012 at 8:34
  • You are right. I was just testing the code. The thing is, I'll try to do like Khanh said. Cheers Commented Jul 6, 2012 at 15:06

2 Answers 2

1

Your code doesn't reflect what you're describing ( > 50 and == 73 are kind of intersecting):

if (section == 0) {
    for (int i = 0; i < [[[BNRItemStore sharedStore] allItems] count]; i ++) {
        ...
        if ([item valueInDollars] > 50) {
            ...
        }
    }
}else{
    for (int i = 0; i < [[[BNRItemStore sharedStore] allItems] count]; i ++) {
        ...
        if ([item valueInDollars] == 73) {
            ...
        }
    }
}

And this line is incorrect too:

BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];

because the indexPath.row will go with an indexPath.section (it means the row is relative to the section, not to the whole table). This is main cause of your problem having the same results for both sections.

Anyway, my suggestion for you is to perform a preprocessing step (maybe in viewDidLoad or somewhere else) to split your array into 2 arrays (one for each section) instead of using only one array for both sections.

Sign up to request clarification or add additional context in comments.

6 Comments

The code that you said is an intersectio it is a way that I found to count the rows of each section. If I didn't missunderstand this method will be called twice, as I have 2 sections, am I wrong? And the second part, is there a way to tell to print in an row in a specifc section? Thanks
Yes, it will be called twice. And for your second question, no, when loading the UITableView, it will ask you to set up the cell at each section and row (of the section). That's you aren't likely to be able to change (and should not actually). As I mentioned, it's better that you split your array into 2 arrays, the call the UITableView to reloadData.
Thanks mate. So you reckon the best place to split into two arrays is in the viewDidLoad method? Thanks again
not really in viewDidLoad. Just that after you have loaded the data, split it into 2 arrays and call the table view to reload.
But can I load the data into viewDidLoad and then slipt it inside this method? Another question that I have is, this method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called all the time that the table is shown? Cheers
|
0

If you are using a NSFetchedResultsController you can use the sectionNameKeyPath: argument to specify a "group-by" parameter. In your case you might just create a simple 0/1 property to your objects in the array.

[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                    managedObjectContext:_managedObjectContext 
                                      sectionNameKeyPath:@"threshold"
                                               cacheName:@"Root"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.