Xamarin / MonoTouch UITableView缓存

时间:2013-04-11 11:08:52

标签: uitableview xamarin.ios uilabel xamarin

我有一个UITableView,我正在填充图像和标签。在我的源类中,我为每个单元格创建了一个UILabel:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

        //if there are no cells create a new one
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
        cell.UserInteractionEnabled = false;

        //create a new cellobject - this grabs the image and returns a CGBitmapContext
        CellObject _cellObject = new CellObject();
        cell.ImageView.Image = _cellObject.DrawCell(treasures[indexPath.Row].cellImage);

        //add text
        UILabel secondViewLabel = new UILabel();
        secondViewLabel.Text = treasures[indexPath.Row].cellTitle;
        Console.WriteLine("The title is: " + treasures[indexPath.Row].cellTitle); 
        secondViewLabel.TextColor = UIColor.White;
        secondViewLabel.TextAlignment = UITextAlignment.Center;
        secondViewLabel.Lines = 0;
        secondViewLabel.LineBreakMode = UILineBreakMode.WordWrap;
        secondViewLabel.Font = UIFont.FromName("Helvetica", 16);
        secondViewLabel.BackgroundColor = UIColor.FromRGB(205, 54, 51);

        //get the width of the text
        SizeF labelSize = secondViewLabel.StringSize(secondViewLabel.Text, secondViewLabel.Font);

        secondViewLabel.Frame = new RectangleF(0, 110 - (labelSize.Height + 10), labelSize.Width + 20, labelSize.Height + 10);

        //add a second view
        UIView secondView = new UIView();
        secondView.AddSubview(secondViewLabel);
        cell.ContentView.AddSubview(secondView);


        return cell;
    }

它完美地构建了表格,但似乎每个单元格都为每个其他单元格获取UILabel以及它自己的单元格。我可以在较短的标签上看到这个,你可以看到背后的其他标签。我正在加载XML并将其解析为列表,然后处理该列表:

List<Treasure> treasures = new List<Treasure>();

    protected class Treasure {
        public string cellTitle { get; set; }
        public string cellTag { get; set; }
        public string cellImage { get; set; }
        public string audioFile { get; set; }
        public string mainTitle { get; set; }
        public string mainTag { get; set; }
        public string mainBody { get; set; }
        public string mainImage { get; set; }
        public string mainCaption { get; set; }
    }

    public CellSource (/*string[] items*/)
    {
        Console.WriteLine("CellSource called");
        string fileName = "treasuresiPhone.xml";
        XDocument doc = XDocument.Load(fileName);
        treasures = doc.Descendants("treasures").FirstOrDefault().Descendants("treasure").Select(p=> new Treasure() {
            cellTitle = p.Element("celltitle").Value,
            cellTag = p.Element("celltagline").Value,
            cellImage = p.Element("cellimage").Value
        }).ToList();

        numCells = treasures.Count();

    }

任何想法或建议都将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:1)

每个UILabel调用创建新的GetCell有点弱:

  • 创建新UI对象并在常用方法中设置其属性的时间;
  • GC可能会占用您的UILabel因为没有明确的链接存在。

尝试其他解决方案:

  • 创建自己的UITableViewCell;
  • 子类
  • 然后,在其中声明UILabel属性;
  • 然后,当出列单元格尝试将单元格转换为您的类型时。如果它不为null,则将其标签的text属性设置为新值。

答案 1 :(得分:1)

如果您将单元格出列,则它已经包含您添加的子视图(UILabels)。单元格整体序列化,包含您在创建时添加的所有视图。 因此:如果您不想继承UITableViewCell,请将标记值分配给自定义视图,并使用ViewWithTag来访问它们。

答案 2 :(得分:0)

这似乎对我有用:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

        cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
        cell.UserInteractionEnabled = false;
        UILabel secondViewLabel = new UILabel();

        //if there are no cells create a new one
        if (cell == null) {
            Console.WriteLine("cell == null");
        } else {

            //create a new cellobject - this grabs the image and returns a CGBitmapContext
            CellObject _cellObject = new CellObject();
            cell.ImageView.Image = _cellObject.DrawCell(treasures[indexPath.Row].cellImage);

            //add text
            secondViewLabel.Tag = 1;
            secondViewLabel.Text = treasures[indexPath.Row].cellTitle;
            Console.WriteLine("The title is: " + treasures[indexPath.Row].cellTitle); 
            secondViewLabel.TextColor = UIColor.White;
            secondViewLabel.TextAlignment = UITextAlignment.Center;
            secondViewLabel.Lines = 0;
            secondViewLabel.LineBreakMode = UILineBreakMode.WordWrap;
            secondViewLabel.Font = UIFont.FromName("Helvetica", 16);
            secondViewLabel.BackgroundColor = UIColor.FromRGB(205, 54, 51);

            //get the width of the text
            SizeF labelSize = secondViewLabel.StringSize(secondViewLabel.Text, secondViewLabel.Font);

            secondViewLabel.Frame = new RectangleF(0, 110 - (labelSize.Height + 10), labelSize.Width + 20, labelSize.Height + 10);

            //add a second view
            UIView secondView = new UIView();
            secondView.AddSubview(secondViewLabel);
            cell.ContentView.AddSubview(secondView);
        }
        return cell;
    }