2

我创建了一个自定义单元格并添加了一个标签和图像,我的表中有 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;
      }
}

问候兰吉特

4

3 回答 3

5

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

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

如果是,它也适用于您的icon财产UIImageView

于 2012-05-15T13:42:15.160 回答
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"]];

    }
于 2012-05-15T13:51:53.763 回答
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]]];
}
于 2016-10-17T10:58:12.407 回答