2013-04-11 33 views
2

我有一個UITableView和子類UITableViewSource類:UITableView RowSelected沒有被調用?

table = new UITableView(window.Bounds); 
table.Source = new CellSource(); 

public class CellSource : UITableViewSource 
{ 
    // etc etc 

我試圖讓所選行,所以在我的源類實現的RowSelected方法:

public override void RowSelected(UITableView tableview, NSIndexPath indexpath) { 
    Console.WriteLine("User tapped"); 
} 

然而,輕敲單元完全沒有響應,即使只是試圖寫入控制檯也不行。

我可以發佈完整的類,如果這將有所幫助嗎?

謝謝。

編輯

所以,我創建後我UITableView

table = new UITableView(window.Bounds); 
table.AutoresizingMask = UIViewAutoresizing.All; 
table.SeparatorStyle = UITableViewCellSeparatorStyle.None; 
table.BackgroundColor = UIColor.Clear; 
table.Source = new CellSource(); 

我用我的源類解析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();  
} 

我然後在另一個類中創建一個CGBitmapContext,返回圖像,並將其設置爲單元格圖像:

public UIImage DrawCell (string cellImage) { 
    string cellI = cellImage; 
    //create a new graphics context 
    int width = 320; 
    int height = 110; 
    CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, width, height, 8, 4*width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst); 

    //load an image 
    var imagePath = (@cellI); 
    var image = UIImage.FromFile(imagePath).CGImage; 
    ctx.DrawImage(new RectangleF(0, 0, width, height), image); 

    UIImage returnedImage = new UIImage(); 
    returnedImage = FromImage(ctx.ToImage()); 

    return returnedImage; 
} 

我添加一些其它測試的東西一樣的標籤,並重寫RowSelected方法:

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; 
     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; 
} 

public override void RowSelected(UITableView tableview, NSIndexPath indexpath) { 
    Console.WriteLine("User tapped"); 
} 
+0

你是否也將委託或數據源分配給您的視圖? – Jason 2013-04-11 14:49:36

+0

我猜不是,我只有CellSource類,它使用XML中的數據填充表。我找不到任何關於分配DataSource的具體內容,我已經Google了,但所有的例子看起來都非常類似於我正在做的事情。感謝回覆。 – mrEmpty 2013-04-11 14:57:19

+0

當你創建表格並分配屬性時,你還在做其他任何事情,還是你發佈了所有內容? – Jason 2013-04-11 15:11:29

回答

2

在MonoTouch的UITableViewSourceUITableView組合UITableViewDataSourceUITableViewDelegate。所以,當你擴展這樣的類時,你可以利用數據源和委託中的方法。

說了這個,我會修改一下你的代碼(見註釋)。

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

     // why do you set the interaction to false? Setting it to false disable interaction for your cell 
     cell.UserInteractionEnabled = true; 

     // create label and view here. You customize the cell with additional elements once 

    } 

    // update image and label here (you need to grab a reference for your label for example through the tag) 

    return cell; 
} 

在Xamarin文檔中也看看Working with Tables and Cells

希望它有幫助。

相關問題