Yadhjira Bertoloni
Yadhjira Bertoloni

Reputation: 33

UITableView Setting different Images

Can somebody help me out with an example code of how to to this: I have my UITableView and I already set everything, I want that when you click on one cell it gives you an image, wich I already have, there are 8 images, and therefore 8 cells with different names each, so what can I do??its for example my cells names, "apple", "orange", "banana" and so on, so I need one picture for each, but of course in order, I mean the banana with the banana and so on. I know i´ts very simple, but I haven´t found any example for this, and I´m very new at all this, Thanks, XD...

Upvotes: 0

Views: 2074

Answers (2)

LJ Wilson
LJ Wilson

Reputation: 14427

What you need is two NSArrays built with the text descriptions and the images (this could also be done with one NSArray and and NSDictionary with two key-value pair). In that case, each dictionary would have two key-value pairs (one for the text and one for the image).

But for the sake of simplicity, we'll use two NSArrays.

So something like this: In viewDidLoad:

textArray = [[NSArray alloc] initWithObjects:@"Banana",
                          @"Orange",
                          @"Apple",
                          @"Grape",
                          @"Pineapple",
                          @"Apricot",
                          @"Pear",
                          @"Kiwi", nil];
imagesArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"Banana.png"],
                        [UIImage imageNamed:@"Orange.png"],
                        [UIImage imageNamed:@"Apple.png"],
                        [UIImage imageNamed:@"Grape.png"],
                        [UIImage imageNamed:@"Pineapple.png"],
                        [UIImage imageNamed:@"Apricot.png"],
                        [UIImage imageNamed:@"Pear.png"],
                        [UIImage imageNamed:@"Kiwi.png"],
                        nil];

* This assumes you have png images in your project named as described above.

Then for the UITableView datasource and delegate methods:

#pragma mark - UITableView Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return count of our text array
    return [textArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvCell"];


    cell.textLabel.text = [textArray objectAtIndex:indexPath.row];
    cell.imageView.image = [imagesArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Do whatever you need to here
}

Complete project can be downloaded HERE (I will leave it up for the next 24 hours)

Upvotes: 3

sarfata
sarfata

Reputation: 4675

There are of course several ways to do this. One of the simplest way which involve no code at all is to use the scenario builder in Xcode.

1 - add a UITableViewController to an application that is using a scenario 2 - select the table view in this controller and in the properties, set it to "static list" (I don't remember the exact name of this property - I will edit my answer to add it if you can't find it) 3 - edit the number of items in the list and each cell to have the label you want 4 - add some UIController to the scene and add the images inside 5 - link each table cell to the matching controller with a segue (ctrl-click and drag or option-click) 6 - click build & run.

That's it - not one line of code!

Ps: I am on my iPhone right now but I could post a full example if you need it. It's actually a pretty cool trick for quick prototypes.

Upvotes: 0

Related Questions