在我的應用程序我必須從一個JSON web服務獲取的圖像和在表視圖顯示它們。雖然向下滾動所有圖像都正確的順序,但是當我向後恢復所有的圖像得到了對方。我正在使用[tableview reloaddata],然後它也發生了。獲取數據
Q
獲取數據
0
A
回答
1
這發生在我在我的應用程序,在那裏我有一個排行榜表。這絕對是因爲表格被重用。
解決這個問題的最簡單方法就是設置單元格的形象,以零,然後再下載和顯示的新形象。
肯定有其他方法可以做到這一點,但我絕對可以保證這一個,因爲我在我自己的應用程序中使用它,它的偉大工程!
下面是如何解決這個問題。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// Make sure you set the image for the cell to nil first
[cell.imageView setImage:nil];
// Then load your new image using the url and display it
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.example.imagedownloadqueue", NULL);
dispatch_async(backgroundQueue, ^(void){
NSUrl * url = [NSUrl UrlWithString:@"www.example.com"]; // Url of image to download
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfUrl:url]];
if (image)
{
dispatch_async(dispatch_get_main_queue(), ^(void) {
[cell.imageView setImage:image]; // Display image after it is downloaded
});
}
});
return cell;
}
+0
非常感謝你的代碼Karim –
+0
@ user3742593根本不是問題。讓我知道它是如何解決你的! –
+0
這段代碼適合你嗎? –
相關問題
這聽起來像是一個細胞回收問題。你可以發佈你的'cellForRowAtIndexPath:'代碼嗎? – chetem
你不應該使用dequeueReusableCellWithIdentifier:在桌面視圖上 –
其實我現在沒有我的代碼,但基本上它由tableview組成,其數量與每個部分的數量和行數一樣多 –