1

I have the following code for my table and my custom cell:

Here is the code of my viewcontroller

public void SearchHotel (){

            Hotel hotel = new Hotel();
            var distribution = new HotelDistribution[]{new HotelDistribution(){ Adults = 1, Children = 0, ChildrenAges = new int[0]} };
            var items = hotel.SearchHotels(Convert.ToDateTime("2013-08-08"),Convert.ToDateTime("2013-09-09 "),"(MIA)", distribution,"","","",0);


            List<DtoHotelinformation> data = new List<DtoHotelinformation>();

            foreach (var item in items)
            {
                DtoHotelinformation DtoHotelinformation = new DtoHotelinformation();
                DtoHotelinformation.price  = item.Price.ToString();
                DtoHotelinformation.title =  item.Name.ToString();
                DtoHotelinformation.subtitle = item.Address.ToString();
                DtoHotelinformation.price = item.Price.ToString();
                DtoHotelinformation.imageUlr = item.ImageUrl;

                data.Add(DtoHotelinformation);
            }

            hud.Hide(true);
            hud.RemoveFromSuperview();
            HotelSearchTable.Source = new HotelTableSource(data.ToArray());
            HotelSearchTable.ReloadData();

        }

Here is the code of my tablesource

    public class HotelTableSource : UITableViewSource
    {

        DtoHotelinformation[] tableItems;
        NSString cellIdentifier = new NSString("TableCell");

        public HotelTableSource (DtoHotelinformation[] items)
        {
            tableItems = items;
        }
        public override int RowsInSection (UITableView tableview, int section)
        {
            return tableItems.Length;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView("Row Selected", tableItems[indexPath.Row].title, null, "OK", null).Show();
            tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight
        }

        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            CustomCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomCell;
            if (cell == null)
                cell = new CustomCell(cellIdentifier);
            cell.UpdateCell(tableItems[indexPath.Row].title, tableItems[indexPath.Row].subtitle, tableItems[indexPath.Row].price,
                            tableItems[indexPath.Row].imageUlr );
            return cell;


        }

        public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return 60;
        }
    }
}

here is the customcell code:

public class CustomCell : UITableViewCell
{
    UILabel headingLabel, subheadingLabel, priceLabel;
    UIImageView imageService;
    UIImageView star, star2, star3, star4, star5;
    public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
    {
        imageService = new UIImageView();
        star   = new UIImageView();
        star2  = new UIImageView();
        star3  = new UIImageView();
        star4  = new UIImageView();
        star5  = new UIImageView();
        headingLabel = new UILabel(){
            Font = UIFont.FromName("Verdana-Bold", 14f),
            BackgroundColor = UIColor.Clear,
            TextColor = UIColor.FromRGB(241, 241, 211)
        };
        subheadingLabel = new UILabel(){
            Font = UIFont.FromName("Verdana-Bold", 8f),
            TextColor = UIColor.FromRGB(60, 0, 100),
            BackgroundColor = UIColor.Clear
        };
        priceLabel = new UILabel(){
            Font = UIFont.FromName("Verdana", 14f),
            TextColor = UIColor.FromRGB(241, 241, 211),
            BackgroundColor = UIColor.Clear
        };
            AddSubview(imageService);
        AddSubview(headingLabel);
        AddSubview(subheadingLabel);
        AddSubview(priceLabel);

    }

    public void UpdateCell (string title, string subtitle, string price, string imageUlr )
    {
        if (imageUlr != null) {
            NSUrl nsUrl = new NSUrl(imageUlr);
            NSData data = NSData.FromUrl(nsUrl);
            imageService.Image = new UIImage(data);
        } else {
            imageService.Image = UIImage.FromFile("generic_no_image_tiny.jpg");
        }

        headingLabel.Text = title;
        subheadingLabel.Text = subtitle;
        priceLabel.Text = price;

    }



    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        imageService.Frame = new RectangleF(10, 10, 50, 33);
        headingLabel.Frame = new RectangleF(70, 4, 220, 25);
        subheadingLabel.Frame = new RectangleF(100, 30, 100, 20);
        priceLabel.Frame = new RectangleF(250, 30, 100, 20);
    }
}

The table works well, I get the data I need, but it's works extremely slow. I mean when I try to scroll the table everything move very slowly.Any idea?

4

3 回答 3

2

不要覆盖GetHeightForRow以返回常量值。而是将RowHeight属性设置UITableView为显示单元格所需的常量值。

这背后的原因是,当您覆盖GetHeightForRowiOSUITableView时,需要为每个单元格查询它以计算总高度。一旦你有数千个潜在的细胞,那将非常缓慢(充其量)并且无法使用。

于 2013-07-22T14:42:38.257 回答
1

避免分配可能很大且易于重用的对象。在这里,您每次要显示单元格时都会创建一个 UIImage。更糟糕的是,您可能正在打开一个文件,读取它并解压缩它,然后创建可显示的 UIImage。您应该使用缓存来避免相同 UIImage 的重复实例。这个小班应该做的工作:

public class ImageCache {
    private Dictionary<string, UIImage> cache = new Dictionary<string, UIImage>();
    public static readonly ImageCache SharedInstance = new ImageCache();

    public ImageCache() {
    }

    private UIImage ImageForUrl(string url) {
        UIImage image = null;

        if (!this.cache.TryGetValue(url, out image)) {
            NSUrl url = new NSUrl(url);
            NSData data = NSData.FromUrl(url);
            image = UIImage.FromData(data);
            if (image != null) {
                this.cache[url] = image;
            }
        }

        return image;
    }

    public UIImage this[string url] {
        get { return this.ImageForUrl(url); }
    }
}

我只是写了它,还没有尝试过,但它应该可以解决问题。像这样使用它

UIImage image = ImageCache.SharedInstance["blabla.png"];

注意:还有一个错误,poupou 解释了您每次更新时添加视图的位置。

于 2013-07-22T20:38:59.403 回答
0

我的问题是图像,这是使用 MonoTouch.Dialog.Utilities.ImageLoader 解决问题的方法

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.Dialog.Utilities;

namespace Zurfers.Mobile.iOS
{
    public class CustomCell : UITableViewCell, IImageUpdated
    {
        UILabel headingLabel, subheadingLabel, priceLabel;
        UIImageView imageService;
        UIImageView star, star2, star3, star4, star5;
        public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
        {
            imageService = new UIImageView();
            star   = new UIImageView();
            star2  = new UIImageView();
            star3  = new UIImageView();
            star4  = new UIImageView();
            star5  = new UIImageView();
            headingLabel = new UILabel(){
                Font = UIFont.FromName("Verdana-Bold", 14f),
                BackgroundColor = UIColor.Clear,
                TextColor = UIColor.FromRGB(241, 241, 211)
            };
            subheadingLabel = new UILabel(){
                Font = UIFont.FromName("Verdana-Bold", 8f),
                TextColor = UIColor.FromRGB(255, 255, 255),
                BackgroundColor = UIColor.Clear
            };
            priceLabel = new UILabel(){
                Font = UIFont.FromName("Verdana", 14f),
                TextColor = UIColor.FromRGB(241, 241, 211),
                BackgroundColor = UIColor.Clear
            };

            AddSubview(imageService);
            AddSubview(headingLabel);
            AddSubview(subheadingLabel);
            AddSubview(priceLabel);
            AddSubview(star);
            AddSubview(star2);
            AddSubview(star3);
            AddSubview(star4);
            AddSubview(star5);

        }

        public void UpdateCell (string title, string subtitle, string price, string imageUlr, string rating )
        {
            if (imageUlr != null) {
                var u = new Uri(imageUlr);
                ImageLoader MyLoader= new ImageLoader(50,50);
                imageService.Image = MyLoader.RequestImage(u,this);

            } else {
                imageService.Image = UIImage.FromFile("generic_no_image_tiny.jpg");
            }


            headingLabel.Text = title;
            subheadingLabel.Text = subtitle;
            if (subtitle.Length > 40) {
                subheadingLabel.LineBreakMode = UILineBreakMode.WordWrap;
                subheadingLabel.Lines = 0;
            }

            switch (rating) {
                case "T":
                    star.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    break;
                case "S":
                    star.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star3.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    break;
                case "F":
                    star.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star3.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star4.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    break;
                case "L":
                    star.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star2.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star3.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star4.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    star5.Image = UIImage.FromFile("ZurfersMovil-Stars.png");
                    break;
            }


            priceLabel.Text = "USD" + price;
            priceLabel.Font = UIFont.BoldSystemFontOfSize (16);
        }


        public void UpdatedImage (Uri uri)
        {
            imageService.Image = ImageLoader.DefaultRequestImage(uri, this);
        }


        public override void LayoutSubviews ()
        {
            base.LayoutSubviews ();
            imageService.Frame = new RectangleF(10, 10, 50, 33);
            headingLabel.Frame = new RectangleF(70, 4, 240, 25);
            subheadingLabel.Frame = new RectangleF(70, 25, 240, 20);
            priceLabel.Frame = new RectangleF(220, 45, 100, 20);
            star.Frame = new RectangleF(70, 45, 15, 15);
            star2.Frame = new RectangleF(85, 45, 15, 15);
            star3.Frame = new RectangleF(100, 45, 15, 15);
            star4.Frame = new RectangleF(115, 45, 15, 15);
            star5.Frame = new RectangleF(130, 45, 15, 15);
        }
    }
}

非常感谢你的帮助。

于 2013-07-23T15:45:28.230 回答