2013-07-03 65 views
2

我有以下CustomCell:Xamarin的MonoTouch CustomCell是空白

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Zurfers.Mobile.iOS 
{ 
    public class CustomCell : UITableViewCell 
    { 
     UILabel headingLabel, subheadingLabel, priceLabel; 
     UIImageView imageService; 
     UIImageView star, star2, star3, star4, star5; 
     public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId) 
     { 
      imageService = new UIImageView(); 
      star = new UIImageView(); 
      star2 = new UIImageView(); 
      star3 = new UIImageView(); 
      star4 = new UIImageView(); 
      star5 = new UIImageView(); 
      headingLabel = new UILabel(){ 
       Font = UIFont.FromName("Cochin-BoldItalic", 22f), 
       TextColor = UIColor.FromRGB (127, 51, 0), 
      }; 
      subheadingLabel = new UILabel(){ 
       Font = UIFont.FromName("Cochin-BoldItalic", 22f), 
       TextColor = UIColor.FromRGB (127, 51, 0), 
      }; 
      priceLabel = new UILabel(){ 
       Font = UIFont.FromName("Cochin-BoldItalic", 22f), 
       TextColor = UIColor.FromRGB (127, 51, 0), 
      }; 
     } 

     public void UpdateCell (string title, string subtitle, string price, UIImage image) 
     { 
      imageService.Image = image; 
      headingLabel.Text = title; 
      subheadingLabel.Text = subtitle; 
      priceLabel.Text = price; 
     } 

     public override void LayoutSubviews() 
     { 
      base.LayoutSubviews(); 
      imageService.Frame = new RectangleF(63, 5, 33, 33); 
      headingLabel.Frame = new RectangleF(5, 4, 63, 25); 
      subheadingLabel.Frame = new RectangleF(100, 18, 100, 20); 
      priceLabel.Frame = new RectangleF(250, 18, 100, 20); 
     } 
    } 
} 

這是我TableSource:

using System; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Zurfers.Mobile.iOS 
{ 
    public class HotelTableSource : UITableViewSource 
    { 

     string[] tableItems; 
     NSString cellIdentifier = new NSString("TableCell"); 

     public HotelTableSource (string[] items) 
     { 
      tableItems = items; 
     } 
     public override int RowsInSection (UITableView tableview, int section) 
     { 
      return tableItems.Length; 
     } 

     public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
     { 
      new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show(); 
      tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight 
     } 

     public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) 
     { 

      CustomCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomCell; 
      if (cell == null) 
       cell = new CustomCell(cellIdentifier); 
      cell.UpdateCell(tableItems[indexPath.Row], tableItems[indexPath.Row], tableItems[indexPath.Row], 
          UIImage.FromFile (tableItems[indexPath.Row])); 
      return cell; 


     } 
    } 
} 

但是當我運行應用程序的TableView中是空的。在創建TableView之前,每個CustomCell都有一個值。

我在做什麼錯?

在此先感謝您的幫助。

回答

4

細胞不僅應該包含控制,它們應該放在它上面(或在它的ContentView)。呼叫AddSubview任何控件,您添加到您的自定義單元格:

ImageService = new UIImageView(); 
AddSubview(ImageService); 

如果沒有幫助,嘗試使用UITableViewCell的默認控件(TextLabelDetailTextLabelImageView)來顯示一些數據。如果數據出現,您的自定義UITableViewCell(錯誤幀,空文本)仍然存在問題,如果不是,則問題在UITableViewSource(空數據)。

+0

謝謝,addSubview的作品。 – Jean