ios пользовательская ячейка на столе не показывает изображение - PullRequest
0 голосов
/ 12 марта 2012

У меня есть пользовательская ячейка, загруженная из моей таблицы, с изображением и меткой, метка показывает нормально, но изображение не показывает

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,

что не хватает? спасибо!

Ответы [ 2 ]

1 голос
/ 12 марта 2012

Ваша переменная tingo равна нулю, когда вы впервые создаете экземпляр ячейки. Выполнение cell.tingo = [NSString stringWithFormat:@"picsButtonImg.png"]; устанавливает его в значение, но тогда ваша клетка никогда не узнает, как сбросить изображение.

Переместите vintageScreen в переменную вашего класса ячеек, затем переопределите ваш tingo установщик, чтобы перезагрузить изображение, если вы хотите сохранить код, который у вас есть в 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]];
}
1 голос
/ 12 марта 2012

self.tingo вероятно, равен нулю в методе initWithStyle. Вы присваиваете свойство self.tingo после вызова initWithStyle.

Эта причина [UIImage imageNamed:self.tingo] также равна нулю, а изображение просто не существует.

Вы можете исправить это, например, с помощью пользовательского установщика свойства tingo или сделав vintageScreen свойством и установить изображение «извне».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...