Stuck on Bronze Challenge - Need a hint


#1

So I’m working on the Bronze Challenge and am just stuck and hoping someone can give me small push in the right direction. I’m having trouble accessing the correct index of the allItems array once the tableview gets beyond the first (0) section. The [indexPath row] goes to 0 when it moves to the section 1. Is there another method I need to use or does one just use brute force to compute the proper array index? Another method I thought of is creating a separate array for each section.

Thanks in advance.


#2

OK. Since I didn’t get any help overnight and couldn’t get any sleep, I forged ahead with two solutions. The first uses simple iteration through the sections and rows to determine an array index, shown immediately below. The second solution creates two arrays and puts them into a main array. The second solution seems much nicer and uses much less code. Is this better? The second method also assumes one know exactly how many sections there will be. What if this isn’t the case? Anyway, I’d still like some expert analysis if anyone has some time. Thanks.

//Creating the sorted array in the init method at top
sortedArray = [[NSMutableArray alloc] init];
    
    for (BNRItem *p in [[BNRItemStore sharedStore] allItems])
    {
        if ([p valueInDollars] <= 50)
        {
            [sortedArray insertObject:p atIndex:0];
        } else if ([p valueInDollars] > 50)
        {
            [sortedArray addObject:p];
        }
    }
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    for (BNRItem *p in [[BNRItemStore sharedStore] allItems])
    {
        NSInteger value = [p valueInDollars];
        
        if (section == 0 && value <= 50)
        {
            numberOfRows++;
        } else if (section == 1 && value > 50)
        {
            numberOfRows++;
        }
    }
        
    return numberOfRows;
}

-(NSInteger)getArrayIndexForIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
{
    arrayIndex = -1;
    NSInteger currentSection = [indexPath section];
    for (int i = 0; i < currentSection + 1; i++)
    {
        if (i == currentSection)
        {
            for (int j = 0; j < [indexPath row] + 1; j++)
            {
                arrayIndex++;
            }
        } else
        {
            NSInteger numberOfRows = [self tableView:tableView numberOfRowsInSection:currentSection];
            for (int j = 0; j < numberOfRows + 1; j++)
            {
                arrayIndex++;
            }
        }
    }
    return arrayIndex;
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                       reuseIdentifier:@"UITableViewCell"];
    }
    
    arrayIndex = [self getArrayIndexForIndexPath:indexPath inTableView:tableView];
    
    BNRItem *p = [sortedArray objectAtIndex:arrayIndex];
    [[cell textLabel] setText:[p description]];
    
    return cell;
}

#3

I’m no expert but I went with your second method. My thinking was that a tableView should get passed only 1 array and indexPathing through it handles slotting the correct strings. In BNRItemStore I sorted the items into a >50 array and a <50 array then put both arrays into the allItems array.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return [[[BNRItemStoresharedStore]allItems] count];

}

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

NSArray *headers = [NSArrayarrayWithObjects:@"Items Under 50", @"Items Over 50", nil];

return [headers objectAtIndex:section];

}

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

return [[[[BNRItemStore sharedStore]allItems] objectAtIndex:section]count];

}

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

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

if(!cell){
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
cell.textLabel.text = [[[[BNRItemStore sharedStore].allItems objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] description];

return cell;
}