2013-07-03 36 views
1

我知道這已經被廣泛討論過了,我敢肯定,我只是錯過了一些東西,但是像我之前的許多其他人一樣,我遇到了單元重用問題一個UITableView ... 我有一個按鈕,顯示一個頭像,我第一次異步下載,然後將其緩存以備後用...... 當我在第一次下載圖像時在我的tableview中滾動,化身錯誤地出現在被重用細胞,儘管我設置化身一個默認的頭像在開始下載前,所以我在這裏希望有人會知道解決的辦法......iOS:UITableViewCell重用,改變按鈕的圖像

在的cellForRowAtIndexPath:

WaitingCell *waitingCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierWaiting]; 

if (waitingCell == nil) { 
    waitingCell = [[WaitingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierWaiting]; 
    waitingCell.delegate = self; 
} 

[waitingCell.userButton setImage:[UIImage imageNamed:DefaultAvatar] forState:UIControlStateNormal]; 
[waitingCell.usernameLabel setText:username]; 
[waitingCell.scoreLabel setText:score]; 

__block UIImage *img; 

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(queue, ^{ 
    if([p1.objectId isEqualToString:currentUser.objectId]) 
    { 
     img = [profileImages getProfileImageForUser:[p2 objectId]]; 
     waitingCell.user = p2; 
    } else 
    { 
     img = [profileImages getProfileImageForUser:[p1 objectId]]; 
     waitingCell.user = p1; 
    } 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [waitingCell.userButton setImage:img forState:UIControlStateNormal]; 
    }); 
return waitingCell; 

「userButton」的圖像是在圖像被完全緩存之前被重用的部分,但我認爲通過將其設置爲默認值(DefaultAvatar是一個常量文件名),它將簡單地設置默認頭像直到對方被下載,它因此不會再使用其他圖片..

該按鈕在我的自定義的UITableViewCell的所謂WaitingCell這樣:

self.userButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [self.userButton setImage:[UIImage imageNamed:PlaceholderAvatar] forState:UIControlStateNormal]; 
    self.userButton.frame = CGRectMake(20, 8, 51, 54); 
    [self.contentView addSubview:self.userButton]; 

任何想法如何,我可以把它重用圖片並直接使用DefaultAvatar直到下載正確的圖像?

+0

您是否通過此設置了斷點或跟蹤?在waitingCell = [tableView dequeueReusableCellWithIdentifier:'''waiting'ell' ever'nil' after'waitingCell = [tableView dequeueReusableCellWithIdentifier:'? –

+0

什麼是completeCell?你在哪裏以及如何定義它? – rdelmar

+0

rdelmar,啊我的錯誤,那只是一個C&P錯誤,因爲我後面有另一個單元命名,而且我在那裏錯誤地選擇了C&P - 但在我的代碼中它正確地命名爲waitingCell :)也編輯了上面的代碼... Marcus,所有行在第一次遇到時都是零,但我開始意識到問題可能在於我的緩存而不是單元重用,因爲在設置臨時映像時單元是零。(如果這使得任何感覺到你) – user969043

回答

0

好吧,所以我找到了另一種方法,而不是我目前使用的方法...主要問題是,因爲我使用後端服務Parse,我必須先下載一些其他信息,然後才能得到訪問文件本身,這似乎導致了圖像的一些奇怪的重用,所以我找到了一種方法來在API中對UITableViewCell的子類進行子類化,似乎有幫助...