0

I want to know how to set image in Selected cell row in UITableView.

If I select the first cell, image should display in first cell and later when I select the second cell, image should display in second cell only (and not in first cell)

Basically, image should display in the selected cell only.

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

3 Answers3

2

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
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [UIView animateWithDuration:2.0f animations:^{
    [imageView setHidden:NO];
    } completion:^(BOOL finished) {
        ;
    }];
}
wormlxd
  • 514
  • 3
  • 7
  • Actuly imageview will add in subview.In cellforRowAtIndex two subview like label and image. imv = [[UIImageView alloc]initwithFram:CGRectMake(7,10,20,25}];imv.image = [UIImage imageNamed:@"image.png"]; [cell addsubview:imv]. how to show this subview image when didselected row in UITableView – user2841148 Dec 27 '13 at 10:29
0

You want a toggling type nature for the imageView.

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    [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];
    //set image
    [cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
}

But this is 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];
}
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98