Gold challenge solution


#1

Hi
My solution for this challenge is the next:

  • First, in ItemsViewController.h add

  • Secondly, add the next method to set the height:

[code]- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0;

if([indexPath row] < [[[BNRItemStore sharedStore] allItems] count]) {
    height = 60;
} else {
    height = 44;
}

return height;

}[/code]

  • To change the font size :

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

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

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

if([indexPath row] < [[[BNRItemStore sharedStore] allItems] count]) {
    BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[p description]];
    // Set the font size
    [[cell textLabel] setFont:[UIFont systemFontOfSize:20]];
} else {
    [[cell textLabel] setText:@"No more items"];
}


return cell;

}[/code]

And to set the background:

- (void)viewDidLoad { [[self tableView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]]; }


#2

My solution was similar, except to get the background image I did:

[code]- (void)viewDidLoad
{
UIImageView *backgroundImageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@“background.jpg”]];
[[self tableView] setBackgroundView:backgroundImageView];

}
[/code]


#3

Question :

I saw that there was a property in UITableView called rowHeight.
I can’t find how to use it … is there a way to use it directly, instead of overriding the method
- tableView:heightForRowAtIndexPath:
?

Thanks


#4

I don’t think that’s necessary, since a UITableViewController (such as ItemsViewcontroller) is always a delegate of its UITableView … (chapter 9, figure 9.4)


#5

I liked dmddmd’s background solution image but I put it in application:didFinishLaunchingWithOptions:.

If the background image never changes is that a good spot to put it?


#6

[quote=“FreddyF”]Question :

I saw that there was a property in UITableView called rowHeight.
I can’t find how to use it … is there a way to use it directly, instead of overriding the method
[/quote]

In the Apple Developer sample code iPhoneCoreDataRecipes, they also change the row height of their table, and do it within the viewDidLoad() method, so for example:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.rowHeight = 60.0;
}

So I tried that, but then the trouble was that it changed the row height for every row, and didn’t allow me to set the row height of only my specific row that I wanted (or at least, I couldn’t work out a way to easily change it to anything else). Maybe you could still use it, and just have the heightForRowAtIndexPath do the row height for the specific row… (thinks… probably not, since the heightForRowAtIndexPath is going to get called for every row…)


#7

Does this code works with two sections?