我有一個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();
}
任何意見或建議,將不勝感激。
謝謝。