0

I have a UIImageView added as subView in the default UITabelViewCell.
When I click on the cell, the UIImageView should display image the in selected cell.
How to do that?

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //Creating cells
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    }
    //Image
    imv = [[UIImageView alloc]initWithFrame:CGRectMake(7,10, 20, 25)];
    imv.image=[UIImage imageNamed:@"loudspeaker.png"];
    [cell addSubview:imv];
   return cell;
}

What to do in -didSelectRowAtIndexPath:?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//????
}

I want to display image in one cell only (whichever was selected last).

When I click first row, image should display in first row only.
When i click second row image will display in second row only, not in first row.

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
user2841148
  • 711
  • 1
  • 8
  • 11

2 Answers2

1

The cleanest approach would be to subclass UITableViewCell.


#import <UIKit/UIKit.h>

@interface SongCell : UITableViewCell
@property (nonatomic, assign) BOOL isPlaying;
@end

@interface SongCell ()
@property (nonatomic, strong) UIImage *speakerImage;
@end

@implementation SongCell

-(void)layoutSubviews
{
    if (!_speakerImage) {
        // http://commons.wikimedia.org/wiki/File:Speaker_Icon.svg
        self.speakerImage = [UIImage imageNamed:@"speaker_icon.png"];
    }

    if(!self.isPlaying){
        self.imageView.image = nil;
    } else {
        self.imageView.image = _speakerImage;
        self.imageView.hidden =NO;
    }
    [super layoutSubviews];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (!selected) {
        self.isPlaying = NO;
    }
}


-(void)setIsPlaying:(BOOL)isPlaying
{
    _isPlaying = isPlaying;
    [self setNeedsLayout];
}
@end

The UITableViews datasource and delegate implementation

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"SongCell";
    SongCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SongCell *cell =(SongCell *)[tableView cellForRowAtIndexPath:indexPath] ;
    cell.isPlaying = !cell.isPlaying;
}

I prepared a demo project for you. You will find it on GitHub

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

Try this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...

    //Create UIImageView object locally (so each cell has separate UIImageView)
    UIImageView *cellSpecificImage = [[UIImageView alloc] initWithFrame:CGRectMake(7,10, 20, 25)];
    [cellSpecificImage setImage:[UIImage imageNamed:@""]];
    [cellSpecificImage setTag:100]; //important

    //add the UIImageView object to the contentView of the cell
    //[cell addSubView:cellSpecificImage]; //incorrect
    [cell.contentView adSubView:cellSpecificImage]; //correct
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //refresh all cells
    //basically call cellForRowAtIndexPath to reset all cells
    [tableView reloadSections:indexPath.section
             withRowAnimation:UITableViewRowAnimationFade];

    //get cell for the currently selected row
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //basically, pointer to the cell's UIImageView object
    UIImageView *tmpCellSpecificImage = (UIImageView *)[cell viewWithTag:100];
    //change parameters and it will manifest on the cell
    [tmpCellSpecificImage setImage:[UIImage imageNamed:@"loudspeaker.png"]];
}

Or... (Alternative Method)

Since your cell is of style UITableViewCellStyleDefault, you need not even create your own UIImageView object because a predefined imageView object is already provided by UITableViewCellStyleDefault (just like textLabel & detailTextLabel).
This imageView object appears on the left side of the cell.

Simply:

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

Example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...

    [cell.imageView setImage: [UIImage imageNamed:@""]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //refresh all cells
    //basically call cellForRowAtIndexPath to reset all cells
    [tableView reloadSections:indexPath.section
             withRowAnimation:UITableViewRowAnimationFade];

    //get cell for the currently selected row
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //change image
    [cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
}

As for having the image displayed in only one cell (depending on which was selected last), I have answered that here (your other question):
How to set image in selected cell row in UITableView

EDIT: infact, i've merged my answers, continued below.


Finally...

The problem with the above method is that it's just a visual aspect.
If you select the first row and scroll down and later return to the first row, the image would no longer be there because -cellForRowAtIndexPath: is recalled and since we had [cell.imageView setImage: [UIImage imageNamed:@""]]; in it, the imageView.image is reset.


To handle the above scenario, you need to remember which rows were selected. (you can achieve this by a basic array)

Example:

- (void)viewDidLoad {
    [super viewDidLoad];

    //arrMyDataSource is defined in .h as 'NSArray *arrMyDataSource;'        
    //needless to say but this is the tableView's datasource contents
    arrMyDataSource = @[@"Apple",@"Banana",@"Candy",@"Door",@"Elephant"];

    //arrMemoryMap is defined in .h as 'NSMutableArray *arrMemoryMap;'
    //this is one way to remember which row is selected
    arrMemoryMap = [NSMutableArray alloc] init];
    for (int i = 0 ; i < arrMyDataSource.count ; i++) {
        NSNumber *tmpSelectStatus = [NSNumber numberWithBool:NO];
        [arrMemoryMap addObject: tmpSelectStatus];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...

    if([[arrMemoryMap objectAtIndex:indexPath.row] boolValue] == YES) {
        [cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
    } else {
        [cell.imageView setImage: [UIImage imageNamed:@""]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //first, reset arrMemoryMap for all rows
    for (int i = 0 ; i < arrMemoryMap.count ; i++) {
         if([[arrMemoryMap objectAtIndex:i] boolValue] == YES]) {
             [[arrMemoryMap objectAtIndex:i] setBoolValue: NO];
             break;
         }
    }

    //now, set current row as selected in arrMemoryMap
    [[arrMemoryMap objectAtIndex:indexPath.row] setBoolValue: YES];

    //reload data or particular section
    [tableView reloadSections:indexPath.section
             withRowAnimation:UITableViewRowAnimationFade];
}
Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • I want to display image in selected cell only,when i click first cell image display in first cell only, when i click second cell image come to second cell only not in first cell. how to do that.. – user2841148 Dec 28 '13 at 08:48
  • @user2841148 : i answered that part in your other question: [link](http://stackoverflow.com/questions/20797887/how-to-set-image-in-selected-cell-row-in-uitableview) – staticVoidMan Dec 28 '13 at 08:54
  • @user2841148 : i've merged both my answers here – staticVoidMan Dec 28 '13 at 09:20