尝试创建一个充满按钮的简单表格,其中按钮是在运行时生成的;该表出现,并且具有正确的行数等,但行似乎是空的。
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 的新手,所以我认为这里缺少一些关于组件大小、定位或布局的东西——有人知道它是什么吗?
最佳答案
通常当我遇到这种问题时,是因为我没有为对象指定框架。因此,在此示例中,尝试在将按钮添加到单元格的 subview 之前设置按钮的框架。例如。
button.Frame = new RectangleF(xcord, ycord, width, height);
请注意,因为您将按钮添加到单元格的 subview 中,所以 x 和 y 坐标是相对于该单元格的。
关于uitableview - 以编程方式添加到 UITableViewCell 的 UIButtons 永远不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3868691/