2010-10-06 66 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

相关问题