UITableView在用户滚动时丢失

问题描述 投票:0回答:3

我正在使用Xamarin在ios中开发移动应用程序。

问题是当你滚动UITableView滞后时它太慢了。

这里我的方法GetCell:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {

        string identifier = @"PostCell";
        var post = Posts [indexPath.Row];
        PostCellVC postCell = tableView.DequeueReusableCell (identifier) as PostCellVC;
        if (postCell == null)
        {
            postCell = new PostCellVC (identifier);
        }
            postCell.btnComments.SetTitle("0 Comments",UIControlState.Normal);
            postCell.lblDescription.Text = post.PostText;
            postCell.btnComments.SetTitle(string.Format("{0} Comments",post.Comments.Length.ToString()),UIControlState.Normal);
            postCell.btnLikes.SetTitle(post.Likes.ToString() + " Likes",UIControlState.Normal);
            postCell.btnUser.SetTitle(post.UserName.ToString(),UIControlState.Normal);
            postCell.imgLike.Image = UIImage.FromBundle ("Images/likeOn.png");
            var actionSheet = new UIActionSheet ("Share options");
            actionSheet.CancelButtonIndex = 2;
            actionSheet.AddButton ("Facebook");
            actionSheet.AddButton ("Twitter");
            actionSheet.AddButton ("Cancel");
            postCell.lblFecha.Text = GetPrettyDate (post.Date);
            //postCell.btnShare.TouchUpInside += share;
            if (post.PictureUrl != null) {
                postCell.imgPost.Image = LayoutHelper.ImageFromUrl (post.PictureUrl);
            }


        return postCell;

    }
ios xamarin.ios xamarin monodevelop xamarin-studio
3个回答
3
投票

您不应在GetCell方法中添加视图或下载图像。当用户滚动表时,会重复调用此方法,如果执行任何长时间运行操作,则会导致延迟滚动。

方法实现应该非常轻量级。应该做的唯一事情是使可重用单元出列或创建新单元并更新该单元的数据。

在您的特定情况下,滞后是通过从远程URL下载照片引起的。此图像下载应该是延迟加载的。理想情况下,下载本身发生在后台线程中,一旦下载了照片,那么只有在UI线程中完成的事情才是用照片更新UIImageView。

对于遇到这样问题的人来说,这是一些很好的资源:https://docs.microsoft.com/en-us/xamarin/ios/user-interface/controls/tables/customizing-table-appearance https://github.com/luberda-molinet/FFImageLoading


4
投票

正如@CRDave指出的那样,你应该尝试延迟加载你的图像。这是在Xamarin中如何做到这一点的sample

另外,为什么要为每个单元格创建一个新的ActionSheet(你不使用它)?由于您一次只能显示一个ActionSheet,只需创建一次并将其用于每个单元格。

最后,尝试重用UIImage“likeOn.png”,而不是从每个单元格的bundle中加载它。


0
投票

请检查此主题Xcode table view lag when scrolling您也可以查看此主题iOS table view lag issue, i'm using dispatch and save cache您应该将数据保存在缓存中并使用dispatch_async

© www.soinside.com 2019 - 2024. All rights reserved.