Bronze challenge issue


#1

I know how to solve this challenge using two separate arrays one storing expensive items and the other storing cheaper items. But i did not want to solve this problem that way. I want two different types of cells wherein type1 points to expensive ones and is adjusted in section0. And type2 pointing to cheaper ones and getting placed in section1.
I’ve attempted this but without any success. Here’s the code:-
This one’s for configuring the number of rows in each section:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    int numberOfRowsWithValueLessThanOrEqualToFifty=0, numberOfRowsWithValueMoreThanFifty=0;
    NSLog(@"No. of BNRItems- %d", [[[BNRItemStore sharedStore] allItems] count]);
    for (BNRItem *item in [[BNRItemStore sharedStore] allItems]) {
        //        BNRItem *p=[[[BNRItemStore sharedStore] allItems] objectAtIndex:i];
        if ([item valueInDollars]>50) {
            numberOfRowsWithValueMoreThanFifty++;
        }
        else
        {
            numberOfRowsWithValueLessThanOrEqualToFifty++;
        }
    }
    
    if (section==0) {
        return numberOfRowsWithValueMoreThanFifty; //no. of rows in section0
    }
    else{
        return numberOfRowsWithValueLessThanOrEqualToFifty;//no. of rows in section1
    }
    
}

This one’s for configuring the two cells and returning either one of them based on specific case:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"hello");
    
    static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifier2 = @"Cell2";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    // Configure the cell...
    if (!cell) {
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (!cell2) {
        cell2= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
    }

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

Now the code has a bunch of problems. If the first if clause runs in conjunction with the second else clause the whole purpose of being selective about what goes in section0 and section1 gets destroyed. In the above code the only valid case would be when the 2 if clauses run together or when 2 else clauses run together . But clearly thats not gonna happen coz the control flow could be random. So does that mean one cannot solve this challenge using two separate types of cells for 2 different sections but have to create separate arrays for storing cheap and expensive items first and then shoving them off into section0 and section1 appropriately?? Pls. advice coz this way was my first idea to solve this problem and i really want it done this way.


#2

My first thought is why are you opposed to using two arrays? In fact one of the better solutions, in my opinion, is to have those two arrays saved in a main array. This way you can access the array by section number.

If you still want to use the method you described above - counting the number of expensive/inexpensive items, I would suggest you do that outside of the tableview data methods. I’d suggest either in an initialization method or viewdidload. Something like that. Then just use the variables in the respective sections. The multiple array method is nicer though

NSArray *subArray = [mainArray objectAtIndex:indexPath.section];

return [subArray count];

As for your other issue with the cells, create one cell and customize it using if statement.