Nicolas Gimelli
Nicolas Gimelli

Reputation: 1352

Embedding UITableView within UIView

I've got a UIView within a UIScrollView, both of which I've created programmatically. The white rectangle is the UIView.

enter image description here

I want to put a UITableView within the UIView so that they're roughly the same size, except I'd like to leave room for the title at the top of the view. Here's what I've tried:

let tableView = UITableView(frame: view1.bounds)
    view1.addSubview(tableView)
    tableView.center(in: view1)
    tableView.rowHeight = 30
    tableView.backgroundColor = .blue

Except nothing shows up. How can I fix this?

Upvotes: 0

Views: 100

Answers (2)

nicog
nicog

Reputation: 112

If you're not instantiating your UIView with a frame: argument, you need to do so. Everything will seem normal if you omit this, and simply use the view1.size() method, except for some reason the tableView will not appear.

Upvotes: 1

Nandish
Nandish

Reputation: 1177

I tried below and I could see the table view.

    let uiView = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
    self.view.addSubview(uiView)

    let tableView = UITableView(frame: uiView.bounds)
    uiView.addSubview(tableView)
    tableView.rowHeight = 30
    tableView.backgroundColor = .blue
    tableView.separatorColor = .black

Below is my result :

enter image description here

You have to remove line tableView.center(in: view1)

Upvotes: 0

Related Questions