Utworzono niestandardową klasę UITableViewCell
z UIButton
, UIImage
i dwiema UILabels
. Przycisk i obraz są nakładane jeden na drugi, a tylko jeden jest wyświetlany na raz. Oczekiwane zachowanie polega na dotknięciu przycisku, przycisk zniknie i wyświetli obraz. UITableViewCells
są ustawione do ponownego użycia. Oto mój kod:Niestandardowe UITableViewCell nie będzie odrysowywać na setNeedsDisplay
Konstruktor:
- (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;
}
}
Przycisk kliknięcie:
- (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];
}
Dzwonię setNeedsDisplay
na wszystko, ale nic się nie dzieje. Jeśli przewijam ekran i robię kopię zapasową, przycisk jest ukryty, a teraz wyświetlana jest ikona ładowania, ale dzieje się tak tylko po przewinięciu. Nie jestem pewien, co muszę zrobić, aby odświeżyć komórkę.
W moim zwyczajem UITableViewCell dodam NSIndexPath i przypisać go do celi, a następnie do ponownego komórkę Ja nazywam to: [reloadRowsAtIndexPaths self.tableView: [NSArray arrayWithObject: currentCell.indexPath] withRowAnimation: UITableViewRowAnimationNone]; –