2010-10-06 36 views
1

嘗試創建一個簡單的表格,其中包含按鈕,這些按鈕是在運行時生成的;該表出現並具有正確數量的行等,但行看起來是空的。以編程方式添加到UITableViewCell中的UIButton永遠不會出現

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 
    { 
     // The NavItem class is a "Josh Bloch enum" with n ordered members 
     NavItem item = NavItem.ByOrder(indexPath.Row); 
     var cell = tableView.DequeueReusableCell(item.Name); 
     if (cell == null) 
     { 
      cell = new UITableViewCell(UITableViewCellStyle.Default, item.Name); 
      UIButton button = UIButton.FromType(UIButtonType.RoundedRect); 
      button.SetTitle(item.Name, UIControlState.Normal); 
      cell.ContentView.AddSubview(button); 
      // cell.TextLabel.Text = item.Name; 
      // cell.Accessory = UITableViewCellAccessory.DisclosureIndicator; 
     } 
     return cell; 
    } 

沒有按鈕,註釋掉的純文本版本可以正常工作。

我是iOS的全新人物,所以我認爲這裏有一些關於組件大小調整或定位或佈局的信息,我錯過了 - 任何人都知道它是什麼?

回答

4

通常當我遇到這樣的問題時,這是因爲我沒有爲對象指定框架。所以在這個例子中,在將它添加到單元格的子視圖之前,嘗試設置該按鈕的框架。例如。

button.Frame = new RectangleF(xcord, ycord, width, height); 

需要注意的是,因爲您將按鈕添加到單元格的子視圖中,所以x和y座標相對於該單元格。

+0

謝謝 - 這似乎這樣做,但現在我需要弄清楚如何設置錶行高度。 :) – 2010-10-06 16:22:43

+0

好的,GetHeightForRow() - 這很容易。 – 2010-10-06 16:31:04

相關問題