首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带图片的UITableView滚动非常慢Xamarin.iOS

带图片的UITableView滚动非常慢Xamarin.iOS
EN

Stack Overflow用户
提问于 2018-06-10 00:45:56
回答 1查看 218关注 0票数 0

我正在使用Xamarin.Ios和Visual Studio for Mac开发一个课程列表应用程序。

我的主应用程序屏幕是一个ExploreTableView,我目前正在列出我通过适当的Azure移动服务简易表格插入的所有课程。在我的课程中,我有一个有点复杂的自定义单元格,它需要显示“TableView Subject”、"TeacherName“、"Lesson Rating”、"Lesson cost“等变量。我几乎已经成功地实现了所有的功能,并且它是有效的。这里是单元格结构:

Cell Preview

我是一名高中IT学生,并不是很擅长xamarin.ios编程,但今天,遵循Youtube微软指南,我还设法实现了Blob存储,我在其中存储了课程封面,我可以检索这些封面,将它们显示在CustomCell的左侧。

问题是,从现在开始,TableView滚动变得非常慢,单元正确地显示了存储在我的Blob Azure存储中的图像,但似乎我在TableView加载单元的方式上做了一些错误的事情。

我试着阅读了一些指南,在堆栈溢出和Microsoft Developer Documentation上,但我真的不能理解缓存系统是如何工作的,以及如何实现它,所以我在这里询问是否有人可以帮助我修复我的代码性能问题,或者建议我一些简单的在线指南。

这是我的ExploreViewController:

代码语言:javascript
复制
using Foundation;
using System;
using System.Collections.Generic;
using UIKit;
using LessonApp.Model;using System.Threading;
using System.Threading.Tasks;

namespace LessonApp.iOS
{
public partial class ExploreViewController : UITableViewController
{
    List<LessonsServices> lessonsServices;

    public LessonsServices lessonService;


    public ExploreViewController(IntPtr handle) : base(handle)
    {
        lessonsServices = new List<LessonsServices>();
    }

    public override async void ViewDidLoad()
    {
        base.ViewDidLoad();

        lessonsServices = await LessonsServices.GetLessonsServices();
        //lessonsServices = await 
        TableView.ReloadData();
    }

    //LISTING ZONE

    public override nint RowsInSection(UITableView tableView, nint section)
    {
        return lessonsServices.Count;
    }


    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell("servicePreviewCell") as LessonsServicesViewCell;

        var lessonService = lessonsServices[indexPath.Row]; 

 //LESSON TITLE
        cell.titleLabel.Text = lessonService.Subject + " Lesson"; //e.g. "Math Lesson"

        //TEACHER NAME AND LOCATION

        cell.teacherNameLocationLabel.Text = lessonService.Teacher + " • " + lessonService.Location;

        // PRO TEACHER BADGE

        switch (lessonService.IsPro)
        {
            case true:
                cell.proLabel.Hidden = false;
                break;

            case false:
                cell.proLabel.Hidden = true;
                break;

            default:
                cell.proLabel.Hidden = true;
                break;

        }

        cell.startingFromPriceLabel.Text = "Starting from " + lessonService.LowestPrice.ToString() + " €/h";

        //Showing Up the Lesson Cover Image in the cell

        var bytes = Task.Run(() => ImagesManager.GetImage(lessonService.Id+".jpeg")).Result; //Here I call the GetImage method, which connects to the Blob Storage Container and retrieve the image that has the same ID of the Lesson Service
        var data = NSData.FromArray(bytes);
        var uiimage = UIImage.LoadFromData(data);
        cell.teacherProfileImageView.Image = uiimage;

        return cell; 

    }


    //I need this Method to force the cell height
    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        return 120;
    }

    //A Segue for another screen, that will copy some information from this page to another  
    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        if (segue.Identifier == "ServiceDescriptionPageSegue")
        {
            var selectedRow = TableView.IndexPathForSelectedRow;
            var destinationViewController = segue.DestinationViewController as ServiceDescriptionView;
            destinationViewController.lessonService = lessonsServices[selectedRow.Row];


        }

        base.PrepareForSegue(segue, sender);
    }
  }
}

感谢您的关注!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 05:48:44

对于在屏幕上可见的每一行,都会调用一次GetCell,并且在滚动期间新行进入视图时会额外调用一次。

您将使用以下命令在GetCell中调用GetImage:

代码语言:javascript
复制
Task.Run(() => ImagesManager.GetImage(lessonService.Id+".jpeg")).Result;

因此,GetCell正在等待GetImage返回,导致滚动速度变慢。

快速的解决方案是使您的GetImage方法异步,并在GetCell中异步调用它,然后在完成后在主线程上调用Image Update。

代码语言:javascript
复制
Task.Run(async () => {
    var bytes = await ImagesManager.GetImage(lessonService.Id + ".jpeg");
    var data = NSData.FromArray(bytes);
    var uiimage = UIImage.LoadFromData(data);
    InvokeOnMainThread(() => {
      cell.teacherProfileImageView.Image = uiimage;
    });
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50776473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档