开发者

UITableView - didSelectRowAtIndexPath Question? [duplicate]

This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

UITableView and Selectin开发者_如何学Gog Items

I am building a iPad app, and have ten Items in a UITableView. When I select option "Apple" I would like it to display a apple in the UIImageView on right. When I select "Orange" I would like it to display a Orange in the UIImageView on the right, and so on.

What is the simplest piece of code to accomplish this?


Assuming you have stored your values that you are displaying in some sort of NSArray or other type of data source (I'm going to assume NSArray), then you could do something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selection = [myArray objectAtIndex:indexPath.row];

    if ([selection isEqualToString:@"Apple"])
    {
        [myImageView setImage:[UIImage imageNamed:@"Apple.png"]];
    }
    elseif ([selection isEqualToString:@"Orange"])
    {
        [myImageView setImage:[UIImage imageNamed:@"Orange.png"]];
    }
}


Building off of Thuggish Nugget's answer, you could incorporate the selection name right into the image string:

NSString *selection = [myArray objectAtIndex:indexPath.row];
[myImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", selection]]];

And you could condense that down into one line.


I answered this alredy for you.

Here is the SO question

It would be a good idea to respond to that same question with a comment if u still need help with a UITableView.

Here is the answer from the other question.


Either set the cell.imageView.highlightedImage, or in the didSelectRowAtIndexPath method, change the cell.imageView.image from nil to some [UIImage imageNamed:@"myImage.png"]:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCustomCellID = @"com.me.foo.cell";
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
    cell.textLabel.highlightedTextColor = [UIColor blueColor];
    cell.imageView.image = nil;
    cell.imageView.highlightedImage = [UIImage imageNamed:@"myImage.png"];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(iPadRootViewControllerTableCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Do this if you didnt set the highlightedImage in cellForRowAtIndexPath
    // [self tableView:tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:@"myApple.png"];
}

Edit (adding comments from other questions answer):

To get a specific item from an array for a row, just use the indexPath.row as the objectForIndex for your array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜