2013-04-11 31 views
0

我有一個UITableView,我用圖像和標籤填充。在我的源類我創建的每個單元一個UILabel:Xamarin/MonoTouch UITableView緩存

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(); 

    } 

任何意見或建議,將不勝感激。

謝謝。

回答

1

創建新UILabel每個GetCell呼叫是有點弱:

  • 時間來創建新的UI對象,並設置它在頻繁調用的方法性能;
  • GC可以吃你的UILabel原因沒有明確的鏈接它存在。

嘗試其他解決方案:

  • 創建您自己的UITableViewCell子類;
  • 然後,在其中聲明UILabel屬性;
  • 然後,當出列單元格嘗試將單元格轉換爲您的類型時。如果不爲null,則將其標籤的文本屬性設置爲新值。
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; 
    } 
1

如果您退出一個單元格,它將已經有您添加的子視圖(UILabels)。該單元格作爲整體序列化,並具有創建時添加的所有視圖。 因此:如果您不想子類UITableViewCell,請將Tag值分配給您的自定義視圖並使用ViewWithTag來訪問它們。