图像仅在滚动 TableView 后出现 [英] Image appears only after scroll TableView

查看:26
本文介绍了图像仅在滚动 TableView 后出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有多个文本字段和图像的 TableView.对于图像加载,我使用 SDWebImage 库.

I have TableView with several text fields and image. For Image loading I use SDWebImage Library.

这是 TableView 源代码

Here is code for TableView Source

public class ExperienceSource : UITableViewSource
{

    //UINavigationController instance, to pass it to constructor
    private UINavigationController primNav { get; set; }
    private UITableView tableView { get; set; }
    List<Experience> TableItems;
    ExperienceTableViewController owner;
    public ExperienceSource(List<Experience> items,ExperienceTableViewController owner, UINavigationController nav)
    {
        TableItems = items;
        this.owner = owner;
        primNav = nav;
    }


    public override nint RowsInSection(UITableView tableview, nint section)
    {

        if (TableItems.Count == 0)
        {


            var noDataLabel = new UILabel
            {
                Text = "No Experiences at your location at this time. Try to change destination",
                TextColor = UIColor.Black,
                TextAlignment = UITextAlignment.Center,
                LineBreakMode = UILineBreakMode.WordWrap,
                Lines = 0
            };
            tableview.BackgroundView = noDataLabel;
            tableview.SeparatorStyle = UITableViewCellSeparatorStyle.None;

            return TableItems.Count;
        }
        else
        {
            tableview.BackgroundView = null;
            tableview.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine;
            return TableItems.Count;
        }


    }

    public override async void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        var selectedExperience = await ExperienceMethods.GetSelectedTour(TableItems[indexPath.Row].id);
        if (selectedExperience == "Saved")
        {
        ExperienceDetailViewController ExperienceDetailController = primNav.Storyboard.InstantiateViewController("ExperienceDetailViewController") as ExperienceDetailViewController;
        primNav.PushViewController(ExperienceDetailController, true);

        }
        else
        {
          UIAlertController okAlertController = UIAlertController.Create("", "Cannot select this experience", UIAlertControllerStyle.Alert);
        okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
        }
        tableView.DeselectRow(indexPath, true);
    }


    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell("cell_id", indexPath) as ExperienceCell;
        Experience item = TableItems[indexPath.Row];
        cell.UpdateCell(item);
        return cell;
    }
}

这里是 CustomCell 的代码

And here is code for CustomCell

public partial class ExperienceCell : UITableViewCell
{
    UIImage picture;
    public ExperienceCell (IntPtr handle) : base (handle)
    {
    }
    internal void UpdateCell(Experience experience)
    {
            var image_url = "https://xplorpal.com/" + experience.cover_image.img_path + "/150x150/" + experience.cover_image.img_file;
            /*using (var url = new NSUrl(image_url))
            using (var data = NSData.FromUrl(url))
            picture = UIImage.LoadFromData(data);
            ExperienceImage.Image = picture;*/
            ExperienceImage.SetImage(NSUrl.FromString(image_url),
                                       UIImage.FromBundle("placeholder"),
                                       SDWebImageOptions.RefreshCached);
            ExperienceTitle.Text = experience.title;
            ExperiencePrice.Text = "$" + experience.price;
    }
    }

我的问题,图像只有在滚动后才会出现在 UIImage 中,或者转到另一个 ViewController 之后,再返回.

My problem, that images appears in UIImage only after scrolling, or go to another ViewController and after, going back.

如果我使用注释代码 vs NSUrl 等等.一切都好.

If I use commented code vs NSUrl etc. All okay.

我的问题在哪里?为什么我使用 SDWebImage 时不显示图像?

Where can be my problem? Why images not appears, when I using SDWebImage?

感谢您的帮助.

推荐答案

具有大量行的表视图在给定时间仅显示其总项的一小部分.表视图仅请求单元格是有效的正在显示的行因此,在滚动表格视图时,单元格会被重新使用,并且单元格内的数据会重新填充.

Table Views with large number of rows display only a small fraction of their total items at a given time.It's efficient for table views to ask for only the cells for row that are being displayed Therefor on scrolling the table view, the cells get re-used and the data within the cell is repopulated.

由于您使用的是下载的图像,因此速度似乎很慢.

Since you are using a downloaded image it appears to be slow.

您可以尝试加载精确大小的图像作为单元格内的图像视图.

You can try loading the image of the exact size as the image view within the cell.

Apple 开发者网站

优化表格视图性能

这篇关于图像仅在滚动 TableView 后出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆