views:

5828

answers:

3

This problem is simple, but crucial and urgent. Here's what needs to be done:

load 66px x 66px images into the table cells in the MainViewController table. each TableCell has a unique image.

But how?

Would we use cell.image?...

cell.image = [UIImage imageNamed:@"image.png"];

If so, where?

Is an if/else statement required? Help?

Here's the project code, hosted on Google Code, for easy and quick reference... http://www.google.com/codesearch/p?hl=en#Fcn2OtVUXnY/trunk/apple-sample-code/NavBar/NavBar/MyCustomCell.m&q=MyCustomCell%20lang:objectivec

To load each cell's labels, MainViewController uses an NSDictionary and NSLocalizedString like so...

 //cell one
    menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
    NSLocalizedString(@"PageOneTitle", @""), kTitleKey,
    NSLocalizedString(@"PageOneExplain", @""), kExplainKey, nil]];

    //cell two
    menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
    NSLocalizedString(@"PageOneTitle", @""), kTitleKey,
    NSLocalizedString(@"PageOneExplain", @""), kExplainKey, nil]];

...

 // this is where MainViewController loads the cell content
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

if (cell == nil)
{
cell = [[[MyCustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCellIdentifier] autorelease];

}

...

    // MyCustomCell.m adds the subviews
- (id)initWithFrame:(CGRect)aRect reuseIdentifier:(NSString *)identifier
{
self = [super initWithFrame:aRect reuseIdentifier:identifier];
if (self)
{
// you can do this here specifically or at the table level for all cells
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

// Create label views to contain the various pieces of text that make up the cell.
// Add these as subviews.
nameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // layoutSubViews will decide the final frame
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.opaque = NO;
nameLabel.textColor = [UIColor blackColor];
nameLabel.highlightedTextColor = [UIColor whiteColor];
nameLabel.font = [UIFont boldSystemFontOfSize:18];
[self.contentView addSubview:nameLabel];

explainLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // layoutSubViews will decide the final frame
explainLabel.backgroundColor = [UIColor clearColor];
explainLabel.opaque = NO;
explainLabel.textColor = [UIColor grayColor];
explainLabel.highlightedTextColor = [UIColor whiteColor];
explainLabel.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:explainLabel];

  //added to mark where the thumbnail image should go 
  imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
  [self.contentView addSubview:imageView];
}

return self;
}

HELP?

+3  A: 

If the image is going to be the same for every cell, i.e., it's part of that type of cell, you could load it in MyCustomCell's init, using self.image = [UIImage imageNamed:"blabla"];

Otherwise, if the image will be different for different cells, it would be more logical to put it in tableView:cellForRowAtIndexPath:

Seventoes
A: 

It works now. You were right, Seventoes, about putting it in tableView:cellForRowAtIndexPath:

indexPath.row was what I was I missing. The working result goes like this:

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

        if (cell == nil)
        {
         cell = [[[MyCustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCellIdentifier] autorelease];

        }

if (indexPath.row == 1)
{
cell.image = [UIImage imageNamed:@"foo.png"];
}
else if (indexPath.row == 2)
cell.image = [UIImage imageNamed:@"bar.png"];
}
...
else
{
cell.image = [UIImage imageNamed:@"lorem.png"];
}

return.cell;
}
Clifton Burt
Is cell.image not depreciated?
ing0
+1  A: 

A better approach then that if-else mess would be to push your images onto a NSMutableArray in the right order, and then just use

cell.image = [myImages objectAtIndex:indexPath.row];

Olof