简体   繁体   中英

UITableView inside UIView

I'm trying to create a dynamic UITableView in a xib, so what I think might work is to put the table view inside a blank UIView. Then I would subclass this UIView and make it adhere to the protocols UITableViewDelegate and UITableViewDataSource . Can someone guide me with this, because I've tried many things, but none of them worked. Many Thanks!

EDIT:

I'll show you what I tried before (sorry, don't have the original code):

class TableControllerView: UIView, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        tableView.frame = self.frame

        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(...cellForRowAt...) {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)

        cell.titleLabel?.text = "test title"

        return cell
    }
}

but in this case the table view ended up being too big and not aligned with the view, even though I set it's frame to be the same as the view

EDIT 2 :

My code now looks like this:

class PharmacyTableView: UIView, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var pharmacyTableView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //PROVISOIRE depends on user [medications].count
    return 2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)

    cell.textLabel?.text = "text label test"
    cell.detailTextLabel?.text = "detail label test"

    return cell
}}

table view is initialized but only shows up when height anchor is constrained, which I don't want because it might grow or shrink depending on user data. I guess I'll be done after solving this? PS : Also, thank you very much to the people that took the time to help me :)

EDIT 3:

So, I've changed the class of the table view to this:

class IntrinsicResizingTableView: UITableView {

override var contentSize:CGSize {
    didSet {
        self.invalidateIntrinsicContentSize()
    }
}

override var intrinsicContentSize: CGSize {
    self.layoutIfNeeded()
    return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}

}

And now everything works fine! Finally!

Your question is unclear. Based on your final statement the following code is what you should use to keep the table view and its superview aligned.

tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

If this is not the answer you're looking for, please clarify your problem. Also it would help if you explain what you're doing this for? Why do you need this superview?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM