Creé una clase personalizada UITableViewCell
con un UIButton
, un UIImage
, y dos UILabels
. El botón y la imagen se superponen uno encima del otro, y solo se muestra uno a la vez. El comportamiento esperado es que toque el botón, el botón desaparece y muestra la imagen. El UITableViewCells
está configurado para ser reutilizado. Aquí está mi código:UITableViewCell personalizado no se volverá a dibujar en setNeedsDisplay
Constructor:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
unheartButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
unheartButton.backgroundColor = [UIColor clearColor];
unheartButton.frame = CGRectMake(10, 13, 20, 18);
[unheartButton addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[unheartButton setBackgroundImage:[UIImage imageNamed:@"redheart.png"] forState:UIControlStateNormal];
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(12, 13, 16, 16);
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int ndx = 1; ndx < 13; ndx++) {
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"icon-loading-%d (dragged).tiff", ndx]]];
}
imageView.animationImages = array;
imageView.animationDuration = 1;
[array release];
[self.contentView addSubview:imageView];
[self.contentView addSubview:unheartButton];
return self;
}
}
- (void) setModel:(MyModel *)myModel {
model = myModel;
if (model.hideImage) {
imageView.hidden = YES;
unheartButton.hidden = NO;
else {
imageView.hidden = NO;
unheartButton.hidden = YES;
}
}
botón de clic:
- (IBAction) onButtonClick: (id) sender {
model.hideImage = NO;
unheartButton.hidden = YES;
imageView.hidden = NO;
[imageView startAnimating];
[self setNeedsDisplay];
[self.contentView setNeedsDisplay];
[self.unheartButton setNeedsDisplay];
[self.imageView setNeedsDisplay];
}
estoy llamando setNeedsDisplay
sobre todo, pero nada parece suceder. Si me desplazo fuera de la pantalla y hago una copia de seguridad, el botón está oculto y ahora se muestra el ícono de carga, pero esto solo ocurre después de un desplazamiento. No estoy seguro de lo que tengo que hacer para que la celda vuelva a pintar.
En mi costumbre UITableViewCell agrego NSIndexPath y asignarlo a la célula, a continuación, volver a dibujar la celda Yo llamo a esto: [reloadRowsAtIndexPaths self.tableView: [NSArray arrayWithObject: currentCell.indexPath] withRowAnimation: UITableViewRowAnimationNone]; –