我有一个 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();
}
任何想法或建议将不胜感激。
谢谢。