Silver Challenge


#1

I did it with one section version, but I could not find any useful methods or constants for the last row, so I did a dirty calculation with screen pixel size and rowHeight size. I’m not sure if it is right way…(but testing shows it works for zero or 20+ items)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // calculate the number of maximal visible rows
    CGFloat height = [tableView rowHeight];
    CGRect frame = [[UIScreen mainScreen] bounds];
    CGFloat maxRows = (int)(frame.size.height / height);
    
    BNRItem *p = [[BNRItem alloc] init];
    NSInteger numberOfAllItems = [[[BNRItemStore sharedStore] allItems] count];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if(!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];

    if(numberOfAllItems > maxRows)
    {
        if([indexPath row] < maxRows - 1)
        {
            p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
            [[cell textLabel] setText:[p description]];
        }
        else if([indexPath row] == maxRows - 1)
        {
            [[cell textLabel] setText:@"No more items!"];
        }
        else
        {
            p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row] - 1];
            [[cell textLabel] setText:[p description]];
        }
    }
    else if([indexPath row] < numberOfAllItems)
    {
        p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
        [[cell textLabel] setText:[p description]];
    }
    else
    {
        [[cell textLabel] setText:@"No more items!"];
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[BNRItemStore sharedStore] allItems] count] + 1;
}