UIButtons programmatically added to UITableViewCell never appear

Question!

Trying to create a simple table full of buttons, where the buttons are generated at run-time; the table appears, and has the right number of rows etc., but the rows appear to be empty.

    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;
    }

Without the buttons, the commented-out text-only version works fine.

I'm brand-new to iOS, so I assume there's something about component sizing or positioning or layout that I'm missing here -- anyone know what it is?



Answers

Usually when I have this sort of issue, it's because I havent specified the frame for the object. So in this example try setting the frame for the button before you add it to the subview of the cell. E.g.

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

Just to note, because you're adding the button to the subview of the cell, the x and y coordinates are relative to that cell.

By : Luke


This video can help you solving your question :)
By: admin