uitableview - 以编程方式添加到 UITableViewCell 的 UIButtons 永远不会出现

标签 uitableview ios uibutton xamarin.ios

尝试创建一个充满按钮的简单表格,其中按钮是在运行时生成的;该表出现,并且具有正确的行数等,但行似乎是空的。

    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/

相关文章:

ios - 适用于 iphone 和 ipad 的应用程序的启动图像

iphone - 这段代码真的需要在 viewDidLoad 末尾有一个 "[self.tableView reloadData]"(在 UITableViewController 中)

ios - 如何处理 UITableView 每一行中每个按钮的按钮单击

iphone - 使用 UISearchBar 时隐藏 UIToolBar

ios - 如何在 superview 中定位 UIView?

objective-c - 如何为所选的UIControlState设置按钮图像

ios - 升级到 iOS8 后,UITableView didSelectRowAtIndexPath 不工作

ios - 适用于 iPhone 和 iPad 的 Xib

iphone - NSoperation如何使用它来等待事件?

ios - 以编程方式创建的 Swift 按钮需要有 2 行标题