0

我有一个 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();

    }

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

谢谢。

4

3 回答 3

1

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

  • 是时候创建新的 UI 对象并在常用方法中设置它的属性了;
  • GC 可能会吃掉你的UILabel原因,但没有明确的链接存在。

尝试其他解决方案:

  • 创建您自己的子类UITableViewCell
  • 然后,UILabel在其中声明属性;
  • 然后,当 dequeue cell 尝试将 cell 转换为您的类型时。如果它不为空,则将其标签的文本属性设置为新值。
于 2013-04-11T14:20:58.333 回答
1

如果您将一个单元格出列,它将已经具有UILabels您添加的子视图 ( )。单元格作为一个整体进行序列化,其中包含您在创建时添加的所有视图。所以:如果您不想子类化UITableViewCell,请将 Tag 值分配给您的自定义视图并用于ViewWithTag访问它们。

于 2013-04-11T17:57:23.917 回答
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;
    }
于 2013-04-11T11:27:26.800 回答