【问题标题】:UIButtons programmatically added to UITableViewCell never appear以编程方式添加到 UITableViewCell 的 UIButtons 永远不会出现
【发布时间】:2010-10-06 00:13:01
【问题描述】:

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

    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 的新手,所以我认为这里缺少关于组件大小、定位或布局的一些东西——有人知道它是什么吗?

【问题讨论】:

    标签: uitableview ios uibutton xamarin.ios


    【解决方案1】:

    通常当我遇到这种问题时,这是因为我没有为对象指定框架。因此,在此示例中,尝试在将按钮添加到单元格的子视图之前设置按钮的框架。例如

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

    请注意,因为您将按钮添加到单元格的子视图,所以 x 和 y 坐标是相对于该单元格的。

    【讨论】:

    • 谢谢 - 似乎可以做到,但现在我需要弄清楚如何设置表格行高。 :)
    • 好的,GetHeightForRow()——这很简单。
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2023-03-03
    • 2018-03-21
    相关资源
    最近更新 更多