CraigBellamy
CraigBellamy

Reputation: 27

Add icon images to UITableview array

I would like to add a different icon image i.e. image1.png, image2.png etc to the following UITableview array. Really need someones help. Thanks in advance.

{
self = [super init];
if (self) {
    // Custom initialization
    self.tableData = [NSArray arrayWithObjects:@"Region", @"Subregion", @"Country",      @"County", @"City", @"District", nil];

    CGRect frame = self.view.bounds;
    frame.size.height -= 100;
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.tableView setScrollEnabled:NO];

    [self.view addSubview:self.tableView]; 
}
return self;
}

Upvotes: 1

Views: 8483

Answers (3)

Lorenzo B
Lorenzo B

Reputation: 33428

You can take advantage of the oop style creating a custom class (say DataItem) and initialize the array you display with DataItem elements. In other words, you could create a model that contain the name and image elements.

For example:

//.h
@interface DataItem : NSObject
{
   NSString* name;
   NSString* thunbmail;
}

@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* thunbmail;

- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail;

@end

//.m
@implementation DataItem

@synthesize name;
@synthesize thunbmail;

- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail
{
   if(self = [super init])
   {
      name = [dName copy]; // release in dealloc!!
      thunbmail = [dThunbmail copy]; // release in dealloc!!
   }
   return self;
}

// create dealloc here

@end

now you can initialize an item like and add it to the array (it could be better to have an NSMutableArray) like the following:

DataItem* di = [[DataItem alloc] initWithName:@"name" withThunbmail:@"image.png"];

NSMutableArray* arrData = [[NSMutableArray alloc] init];
[arrData addObject:di];

// add other object here

self.tableData = arrData;

// release memory...

and then in cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Configure the cell..

    DataItem* di = (DataItem*)[self.tableData objectAtIndex:[indexPath row]];
    cell.textLabel.text = di.name;
    cell.imageView.image = [UIImage imageNamed:di.thunbmail];

    return cell;
}

This is an elegant way to enclose your content in a single class model.

Hope it helps.

P.S. Check the code. I've written by hand.

Upvotes: 1

Radu
Radu

Reputation: 3494

You could create another array that keeps the name of the pictures

self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil];

in the order you want them to be displayed and in the cellForRowAtIndexPath just write

cell.imageView.image = [UIImage imageNamed:[tablePicture objectAtIndex:indexPath.row]];

Upvotes: 4

phi
phi

Reputation: 10733

Use cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Configure the cell.
    cell.textLabel.text = @"cell text";
    cell.imageView.image = [UIImage imageNamed:@"image1.png"];
    return cell;
}

Upvotes: 4

Related Questions