我有一个表视图与背景视图和一些文字。我正在从一个url下载背景(在下面的代码中它是硬编码的,但我将为每个单元格设置不同的url)。为了改善响应,我使用图像缓存来保存下载的图像并异步加载图像。如果我滚动,图像会调整到每个单元中的其他东西,如果我停止滚动,我可以看到图像随机刷新在每个单元有时像刚刚消失,几秒钟后再次出现表视图与异步图像加载代理怪异
我不够好于iOS的图了这一点请大家帮
这里是我的手机数据源的方法:。
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0){
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
[cell.contentView addSubview:self.headerFrame];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
static NSString *cellIdentifier = @"HistoryCell";
// Similar to UITableViewCell, but
CustomSaloonCell *cell = (CustomSaloonCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomSaloonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *identifier = [NSString stringWithFormat:@"Cell%ld" ,
(long)indexPath.row];
if([self.cachedImages objectForKey:identifier] != nil){
cell.backgroundView = [[UIImageView alloc]initWithImage:[cachedImages valueForKey:identifier]];
}
else {
char const * s = [identifier UTF8String];
dispatch_queue_t queue = dispatch_queue_create(s, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/en/thumb/7/73/Shrekcharacter.jpg/220px-Shrekcharacter.jpg"];
UIImage *img = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
if ([theTableView indexPathForCell:cell].row == indexPath.row) {
[cachedImages setValue:img forKey:identifier];
cell.backgroundView = [[UIImageView alloc]initWithImage:[cachedImages valueForKey:identifier]];
cell.backgroundView.alpha = 0.0;
[UIView animateWithDuration:5.0 animations:^{
cell.backgroundView.alpha = 1.0;
}];
}
});
});
}
/********/
cell.stylistName.text = [salonDetailsArray objectAtIndex:indexPath.row][@"salonName"];
cell.salonAddress.text = [salonDetailsArray objectAtIndex:indexPath.row][@"salonAddress"];
cell.delegateListener = self;
cell.claimButton.frame =CGRectMake(494/2, 216/2, 120/2, 46.0/2.0);
[cell.claimButton addTarget:self action:@selector(claimClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.claimButton setTitle:@"claim" forState:UIControlStateNormal];
if([loginStatus isEqualToString:@"YES"]){
[cell.claimButton setTitleColor:[UIColor colorWithRed:106.0/255.0 green:45.0/255.0 blue:118.0/255.0 alpha:1.0] forState:UIControlStateNormal];
}else {
[cell.claimButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
cell.claimButton.tag = indexPath.row;
cell.overlay.hidden = YES;
for (NSIndexPath *selectedIndex in self.buttonStateArray){
if([selectedIndex isEqual:indexPath]){
[cell.claimButton setTitle:@"claimed" forState:UIControlStateNormal];
cell.claimButton.frame = CGRectMake(464/2-(30/2), 216/2, 180/2, 46.0/2.0);
cell.overlay.hidden = NO;
[cell.claimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
return cell;
}
这是SO上的第二个问题。搜索更难时你会找到有价值的答案。 ;)主要的问题是你正在捕获你的完成处理程序中的_cell_ - 可能在处理程序被执行的同时被重用(相反,捕获_indexPath_并再次获取单元格!)。您还应该使用适当的方法来加载远程资源。 – CouchDeveloper