0

I'm using Xamarin to develop a mobile app in ios.

The problem is that when you scrolling the UITableView lags and its too slow.

Here my method 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;

    }
user2743074
  • 33
  • 1
  • 7
  • postCell.imgPost.Image = LayoutHelper.ImageFromUrl (post.PictureUrl); This line make your UI legs. Search "uitableview lazy loading" and you will get many solution. here is one example from Apple : https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html – CRDave Jul 22 '14 at 13:58

3 Answers3

4

As @CRDave points out, you should try lazy loading your images. Here is a sample of how to do this in Xamarin.

Also, why are you creating a new ActionSheet for every cell (which you don't use)? Since you can only show one ActionSheet at a time, just create it once and use it for every cell.

Finally, try reusing UIImage for "likeOn.png" instead of loading it from the bundle for every cell.

Jason
  • 86,222
  • 15
  • 131
  • 146
3

You shouldn't add views or download images in your GetCell method. This method is called repeatedly as the user scrolls the table, if it does any long-running operation, it will lead to laggy scrolling.

The method implementation should be really lightweight. The only things that should be done is to dequeue a reusable cell or create a new one and update the data for that cell.

In your specific case, the lag is cause by downloading a photo from a remote URL. This image download should be lazy loaded. Ideally the download itself occurs in a background thread and once the photo is downloaded, then only thing done in the UI thread is updating the UIImageView with the photo.

A few good resources for anyone experiencing issues like this: https://learn.microsoft.com/en-us/xamarin/ios/user-interface/controls/tables/customizing-table-appearance https://github.com/luberda-molinet/FFImageLoading

pnavk
  • 4,552
  • 2
  • 19
  • 43
0

Please check this topic Xcode table view lag when scrolling also you can check this topic iOS table view lag issue, i'm using dispatch and save cache you should save your datas on cache and also use dispatch_async

Community
  • 1
  • 1
Gökhan Çokkeçeci
  • 1,388
  • 3
  • 16
  • 37