UITableViewCell + SDWebImage在滚动时不显示/保留图像

时间:2017-05-02 03:38:55

标签: ios uitableview xamarin.ios uiimageview sdwebimage

我有UITableView表格来源和自定义单元格。自定义单元格由UIImageView和两个标签组成。必须从需要下载身份验证令牌的网址下载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天以来,我完全对这个问题感到沮丧,我们非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

使用此行创建一个单元格,如果找到则会使用现有单元格,否则会为您创建一个新单元格。

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()方法中删除图像下载代码。