James Raitsev
James Raitsev

Reputation: 96391

Objective-C, UITableView, clarification needed

My program has 2 NSMutableArray(s) each containing a list of items. Some cost over $50, some cost less then $50. The task is to display this information as part of a table. So ..

My table has 2 sections

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

Each section has a name

- (NSString *)tableView:(UITableView *)
     tableView titleForHeaderInSection:(NSInteger)section {

    NSString *lSectionTitle;
    switch (section) {
        case 0:
            lSectionTitle = @"Under $50";
            break;
        case 1:
            ... 

Each section has a count

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

    switch (section) {
        case 0:
            count =  [[[PossessionStore defaultStore] under50Possessions] count];
            break;
        case 1:
            .....

And then finally we figure out what to display as part of a given cell

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

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

    Possession *p = [[[PossessionStore defaultStore] allPossessions] objectAtIndex:[indexPath row]];

    [[cell textLabel] setText:[p description]];

    return cell;
}

The code above references allPossessions, which consists of both items over $50 and under $50. I am stuck at this point.

In this example, is there a way for me to know whether i am being asked to draw a cell for under or over $50 category?

Upvotes: 0

Views: 74

Answers (1)

Caleb
Caleb

Reputation: 124997

All is well, but i really don't understand how does UITtable "knows" to place items under and over $50 into the correct category.

It doesn't. It looks like you might just be getting lucky in that the way the PosessionStore returns the data from -allPossessions has the possessions organized by price, or something. Your -tableView:cellForRowAtIndexPath: method should really be taking the section as well as the row into account when populating each cell.

Upvotes: 1

Related Questions