在UITableViewcell的选定状态下更改图像

时间:2012-05-15 13:38:25

标签: ios ios5

我创建了一个自定义单元格并添加了一个标签和图像,我在表格中有4行,每行有不同的图像,每行打开一个不同的视图控制器,所以,现在我需要点击一个特别的一行我希望图像改变为我尝试过这样做,但它没有用,所以请帮助我。

if(indexPath.row == 0)
{

     if(cell.selected == true)
     {
           UIImage *cellImage = [UIImage imageNamed:@"abc.png"];        
           cell.icon.image    = cellImage;
      }
      else
      {
          UIImage *cellImage = [UIImage imageNamed:@"abc.png"];        
           cell.icon.image    = cellImage;
      }
}

此致 Ranjit

3 个答案:

答案 0 :(得分:5)

尝试在创建单元格时或在tableView:willDisplayCell:forRowAtIndexPath:方法中执行以下操作:

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

如果是icon

,它也适用于您的UIImageView媒体资源

答案 1 :(得分:2)

首先在uiImageview的自定义单元格中创建一个属性并合成它。

和UITabeView的didSelectRowAtIndexPath委托方法访问属性并更改图像: -

yourCell.yourImageView.image=[UIImage imageNamed:@"yourImage"]

对于样品我给出了我的代码: -

#import <UIKit/UIKit.h>


@interface CustomizedCellProductDetails : UITableViewCell {

    UILabel *sNO;
    UILabel *abcWine;
    UILabel *redWine;
    UILabel *two;
    UILabel *hundred;
    UILabel *fourTwo;
    UILabel *twoOne;
    UIImageView *imgView;

    UILabel *itemNo;
    UILabel *itemName;
    UILabel *itemDesc;
    UILabel *department;
    UILabel *qtyAvailable;

    UIButton *check;

}

@property (nonatomic , retain) UILabel *sNO;
@property (nonatomic , retain) UILabel *abcWine;
@property (nonatomic , retain) UILabel *redWine;
@property (nonatomic , retain) UILabel *two;
@property (nonatomic , retain) UILabel *hundred;
@property (nonatomic , retain) UILabel *fourTwo;
@property (nonatomic , retain) UILabel *twoOne;
@property (nonatomic , retain) UIImageView *imgView;

@property (nonatomic , retain) UILabel *itemNo;
@property (nonatomic , retain) UILabel *itemName;
@property (nonatomic , retain) UILabel *itemDesc;
@property (nonatomic , retain) UILabel *department;
@property (nonatomic , retain) UILabel *qtyAvailable;
@property (nonatomic , retain) UIButton *check;

-(void) clicked;
@end

.m文件合成它: -

#import "CustomizedCellProductDetails.h"


@implementation CustomizedCellProductDetails
@synthesize sNO,abcWine,redWine,two,hundred,fourTwo,twoOne,imgView,itemNo,itemName,itemDesc,department,qtyAvailable,check;
tableview委托中的

: -

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
CustomizedCellProductDetails * cell = (CustomizedCellProductDetails )[tableView cellForRowAtIndexPath:indexPath]; 
[cell.imgView setImage:[UIImage imageNamed:@"wine.png"]];

    }

答案 2 :(得分:0)

创建单元格时尝试执行以下操作...

执行区块......

@implementation ABC
{
    NSMutableArray  *imageArray;
    NSMutableArray  *imageSelectedArray;
}

执行以下方法

(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    [cell.imageView setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]];
    [cell.imageView setHighlightedImage:[UIImage imageNamed:[imageSelectedArray objectAtIndex:indexPath.row]]];
}