我有一个从我的桌子加载的自定义单元格,带有图像和标签,标签显示正常,但图像没有显示
CustomCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: (NSString*)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self initLabels]; CGRect vintageScreenRect = CGRectMake(25, 0.0f, 100, 100); self.iconImage = [[UIImage alloc]init]; UIImageView *vintageScreen = [[UIImageView alloc] initWithFrame:vintageScreenRect]; //[vintageScreen setImage:[UIImage imageNamed:@"vidButtonImg.png"]]; // [vintageScreen setImage:self.iconImage]; [vintageScreen setImage:[UIImage imageNamed:self.tingo]]; vintageScreen.opaque = YES; // explicitly opaque for performance [self.contentView addSubview:vintageScreen]; [vintageScreen release]; NSLog(@"tingo ::%@", self.tingo); } return self; }
UsingTable.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.tingo = [NSString stringWithFormat:@"picsButtonImg.png"]; cell.iconName.text = @"d"; } return cell; }
所以标签显示确定,但图片没有显示,我试过发送uiImage和NSString,
缺少什么?谢谢!
答案 0 :(得分:1)
self.tingo
可能在initWithStyle
方法中为零。在initWithStyle调用之后,您正在调用self.tingo
属性。
原因[UIImage imageNamed:self.tingo]
也是零,图像根本不存在。
您可以通过自定义的tingo属性设置器修复它,或者将vintageScreen作为属性并从外部设置图像。
答案 1 :(得分:1)
首次实例化单元格时,变量tingo
为null。执行cell.tingo = [NSString stringWithFormat:@"picsButtonImg.png"];
会将其设置为一个值,但是您的单元格永远不会知道重置图像。
将vintageScreen
移动到您的单元格类的变量中,然后覆盖您的tingo
setter以重新加载图片,如果您想保留cellForRowAtIndexPath:
CustomCell.h
@interface CustomCell : UITableViewCell {
UIImageView *vintageImage;
NSString *tingo;
}
@property (nonatomic, retain) UIImageView *vintageImage;
@property (nonatomic, retain) NSString *tingo;
@end
CustomCell.m
@implementation CustomCell
@synthesize vintageImage, tingo;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initLabels];
[self addSubview:[self vintageScreen]];
}
return self;
}
-(UIImageView *)vintageScreen {
if(vintageScreen == nil) {
CGRect vintageScreenRect = CGRectMake(25, 0.0f, 100, 100);
[self setVintageScreen:[[[UIImageView alloc] initWithFrame:vintageScreenRect] autorelease]];
[vintageScreen setImage:[UIImage imageNamed:self.tingo]];
vintageScreen.opaque = YES;
}
return vintageScreen;
}
-(void)setTingo:(NSString *)newTingo {
[tingo release];
tingo = [newTingo retain];
[[self vintageScreen] setImage:[UIImage imageNamed:tingo]];
}