私はこれが以前に広く議論されたことを知っています.私は単に何かが欠けているだけだと確信しています.アバター、そして私はそれを初めて非同期にダウンロードし、後で使用するためにキャッシュします...画像が初めてダウンロードされている間にテーブルビューをスクロールすると、再利用されているセルにアバターが誤って表示されます.ダウンロードを開始する前にアバターをデフォルトのアバターに設定するので、ここの誰かが解決策を知っていることを願っています...
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 は固定のファイル名) に設定することで、もう一方がダウンロードされるまでデフォルトのアバターを設定するだけで済むと思いましたしたがって、他の画像を再利用しません..
ボタンは、WaitingCell と呼ばれるカスタム UITableViewCell で次のように定義されています。
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 を使用する方法はありますか?