带图像的UITableView滚动非常缓慢Xamarin.iOS

时间:2018-06-09 16:45:56

标签: c# ios uitableview xamarin.ios custom-cell

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

我的主应用程序屏幕是ExploreTableView,我目前正在列出我通过适用的Azure移动服务简易表插入的所有课程。 在我的TableView中,我有一个复杂的自定义单元格,需要显示“Lesson Subject”,“TeacherName”,“Lesson Rating”,“Lesson cost”和其他变量。我几乎已经成功完成了所有工作。这里是细胞结构:

Cell Preview

我是一名高中IT学生,在xamarin.ios编程方面不是很专业,但今天,在Youtube微软指南之后,我还设法实现了Blob存储,我在其中存储了课程内容,我可以检索将它们显示在CustomCell的左侧。

问题是,从现在起,TableView滚动变得非常慢,单元格正确显示存储在我的Blob Azure存储上的图像,但似乎我在TableView加载单元格的方式上做错了。

我试着在Stack Overflow和Microsoft Developer Documentation上阅读一些指南,但老实说我无法理解缓存系统的工作原理以及如何实现它,所以我在这里问如果有人可以帮助我解决我的代码性能问题,或者建议我一些易于遵循的在线指南。

这是我的ExploreViewController:

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);
    }
  }
}

感谢您的关注!

1 个答案:

答案 0 :(得分:1)

为屏幕上可见的每一行调用一次GetCell,并在滚动期间新行进入视图时再调用一次。

您正在使用以下方法在GetCell中调用GetImage:

Task.Run(() => ImagesManager.GetImage(lessonService.Id+".jpeg")).Result;

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

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

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;
    });
});
相关问题