And*_*ner 5 iphone cocoa-touch objective-c iphone-sdk-3.0
我创建了一个UITableViewCell带有a UIButton,a UIImage和two 的自定义类UILabels.按钮和图像叠加在一起,一次只显示一个.预期的行为是您触摸按钮,按钮消失,并显示图像.该UITableViewCells设置可以重复使用.这是我的代码:
构造函数:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
按钮点击:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我正在呼唤setNeedsDisplay一切,但似乎什么都没发生.如果我滚动屏幕并备份,则按钮被隐藏,现在显示加载图标,但这仅在滚动后发生.我不确定我需要做什么来让细胞重新粉碎.
UITableView 做了一些奇特的缓存来支持滚动等;当我使用自定义视图时,我在刷新特定单元格时遇到了类似的问题。
您可以使用 reloadRowsAtIndexPaths:withRowAnimation: 使表仅重新加载该行。有关更多信息,请参阅这篇文章:Why won't my UITableViewCell deselect and update its text?
| 归档时间: |
|
| 查看次数: |
6776 次 |
| 最近记录: |