[英]UITableViewCell + SDWebImage not showing/ persisting images on scroll
我有一個帶有表源和自定義單元格的UITableView
。 自定義單元格包含一個UIImageView
和兩個標簽。 必須從需要下載身份驗證令牌的URL中下載UIImageView
圖像。
我為此使用SDWebImage
。 在必須在單個ImageView
加載圖像之前,我已經成功使用了該庫。 這是我第一次在TableView
使用它,但無法使其正常工作。 這是我嘗試過的
UITableViewSource.cs
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = peopleHomeController.tablePeopleSearch.DequeueReusableCell("PeopleNewSearchCell") as PeopleNewSearchCell ?? PeopleNewSearchCell.Create(); ;
if(peopleHomeController.isSearch)
{
var data = peopleHomeController.peopleList.ElementAt(indexPath.Row);
cell.UpdateCell(data);
}
else
{
var data = peopleHomeController.relatedPeopleList.ElementAt(indexPath.Row);
cell.UpdateCell(data);
}
return cell;
}
PeopleNewSearchCell.cs
public void UpdateCell(CustomValue mValue)
{
labelPeopleEmail.Text = email = mValue.mail;
labelPersonName.Text = mValue.name;
if (!string.IsNullOrEmpty(mValue.mobilePhone))
{
labelPhone.Text = mValue.mobilePhone;
}
else
{
labelPhone.Text = "-";
}
var mUrl = "someimageurl";
var manager = SDWebImageManager.SharedManager;
manager.ImageCache.MaxCacheAge = 86400;
manager.ImageCache.ShouldCacheImagesInMemory = true;
SDWebImageDownloader mDownloader = manager.ImageDownloader;
mDownloader.SetHttpHeaderValue(authToken, "Authorization");
mDownloader.SetHttpHeaderValue("application/json; odata=verbose", "Accept");
mDownloader.SetHttpHeaderValue("f", "X-FORMS_BASED_AUTH_ACCEPTED");
try
{
mDownloader.DownloadImage(
url: new NSUrl(mUrl),
options: SDWebImageDownloaderOptions.UseNSUrlCache,
progressBlock: (receivedSize, expectedSize) =>
{
//Track progress...
},
completedBlock: (image, data, error, finished) =>
{
if (image != null && finished)
{
InvokeOnMainThread(() =>
{
this.imgPeopleProfile.Image = image;
});
}
if (error != null)
{
InvokeOnMainThread(() =>
{
this.imgPeopleProfile.Image = UIImage.FromFile("ic_account.png");
});
}
}
);
}
catch (Exception ex)
{
}
CALayer profileImageCircle = imgPeopleProfile.Layer;
profileImageCircle.CornerRadius = 40;
profileImageCircle.BorderWidth = 2;
profileImageCircle.BorderColor = UIColor.White.CGColor;
profileImageCircle.MasksToBounds = true;
}
public override void PrepareForReuse()
{
base.PrepareForReuse();
imgPeopleProfile.CancelCurrentImageLoad();
imgPeopleProfile.Image = null;
}
使用上面的代碼,圖像已下載但未顯示,但是在滾動時顯示了錯誤的單元格。 另外,當圖像顯示在錯誤的單元格上時,如果我不滾動,則錯誤的單元格圖像也會消失。 (我希望我有道理。)如SO的一些答案所述,我還取消了PrepareForReuse()
上的任何圖像加載。
自2天以來,我對這個問題完全感到困惑,對您的幫助深表感謝。
使用此行創建一個單元格,如果找到,它將使用現有單元格,否則將為您創建一個新單元格。
let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleNewSearchCell", for: indexPath)as! PeopleNewSearchCell
現在在所有方法之外聲明mDownloader
,因為它將在所有方法中都可以訪問,例如iVar:
var mDownloader:SDWebImageDownloader!
然后在您的viewDidLoad
()方法中設置令牌和SDWebImage
其他字段
var manager = SDWebImageManager.SharedManager;
manager.ImageCache.MaxCacheAge = 86400;
manager.ImageCache.ShouldCacheImagesInMemory = true;
mDownloader = manager.ImageDownloader;
mDownloader.SetHttpHeaderValue(authToken, "Authorization");
mDownloader.SetHttpHeaderValue("application/json; odata=verbose", "Accept");
mDownloader.SetHttpHeaderValue("f", "X-FORMS_BASED_AUTH_ACCEPTED");
現在在tableView的數據源方法中,使用mDownloader
下載單元格的圖像:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleNewSearchCell", for: indexPath)as! PeopleNewSearchCell
if(peopleHomeController.isSearch)
{
var data = peopleHomeController.peopleList.ElementAt(indexPath.Row);
cell.UpdateCell(data);
}else{
var data = peopleHomeController.relatedPeopleList.ElementAt(indexPath.Row);
cell.UpdateCell(data);
}
//download an image
try
{
mDownloader.DownloadImage(
url: new NSUrl(mUrl),
options: SDWebImageDownloaderOptions.UseNSUrlCache,
progressBlock: (receivedSize, expectedSize) =>
{
//Track progress...
},
completedBlock: (image, data, error, finished) =>
{
if (image != null && finished)
{
InvokeOnMainThread(() =>
{
cell.imgPeopleProfile.Image = image;
});
}
if (error != null)
{
InvokeOnMainThread(() =>
{
cell.imgPeopleProfile.Image = UIImage.FromFile("ic_account.png");
});
}
}
);
}
catch (Exception ex)
{
}
return cell;
}
並從updateCell
()方法中刪除圖像下載代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.