选定的单元格图像将显示在UITableView中

时间:2013-12-28 06:39:03

标签: ios cocoa-touch uitableview didselectrowatindexpath

我在默认UIImageView中添加了subView UITabelViewCell 单击单元格时,UIImageView应显示所选单元格中的图像 怎么做?

- (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;
}

-didSelectRowAtIndexPath:中做什么?

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

我想只在一个单元格中显示图像(最后选择的那个)。

当我点击第一行时,图像应仅显示在第一行 当我点击第二行时,图像仅显示在第二行,而不是第一行。

2 个答案:

答案 0 :(得分:1)

最干净的方法是继承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

UITableViews数据源和委托实现

-(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;
}

我为你准备了一个演示项目。您可以在GitHub

找到它

答案 1 :(得分:0)

试试这个:

- (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"]];
}

或......(替代方法)

由于您的单元格是样式UITableViewCellStyleDefault,因此您甚至无需创建自己的UIImageView对象,因为{{>预定义的 imageView对象已由{{{ 1}}(就像UITableViewCellStyleDefault&amp; textLabel ) 此detailTextLabel对象显示在单元格的左侧。

简单地:

imageView

示例:

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

  

至于图像只显示在一个单元格中(取决于最后选择的),我已回答here您的其他问题) :
 How to set image in selected cell row in UITableView

 编辑:事实上,我已经合并了我的答案,继续下面。


最后...

上述方法的问题在于它只是一个视觉方面 如果您选择第一行并向下滚动并稍后返回第一行,则图片将不再存在,因为- (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"]]; } 已被召回,而且由于我们已-cellForRowAtIndexPath:[cell.imageView setImage: [UIImage imageNamed:@""]];重置。


要处理上述情况,您需要记住选择了哪些行。 (您可以通过基本数组实现此目的)

实施例

imageView.image

- (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];
    }
}