Puneet
Puneet

Reputation: 301

Create a custom view showing subviews as defined like UITableViewCell

When we create a UITableViewCell in InterfaceBuilder, it shows a content view automatically which you cannot remove. Is there any way that i can create my own custom UIView like that which has a content view of its own ? And is automatically shown in interface builder as well. I dont need any subclass of UITableViewCell but a subclass of UIView

Upvotes: 0

Views: 294

Answers (4)

LuisCien
LuisCien

Reputation: 6432

I have been looking for this as well but haven't been able to figure it out so far. I don't think IB supports it at the moment. I did, however, based on @A_curious_developer' answer find a workaround that works well for me.

TL;DR: The trick is to use the contentView's tag to reference it in code, then use Auto Layout to set the appropriate constraints depending on your needs.

Here's an example:

First, I created my custom view. It has a "bubble/tooltip" at the bottom and an empty view (selected) at the top.

enter image description here

Then, I used this custom view in my view controller and added a subview to it to act as the contentView (white background, selected). I also gave it a tag (12345).

enter image description here

Finally, I look for this tag in my custom view's awakeFromNib and set the appropriate constraints.

override func awakeFromNib() {
    if let view = viewWithTag(12345) {
        theContent = view

        // set constraints here...
    }
}

Hope this helps!

Upvotes: 0

A_Curious_developer
A_Curious_developer

Reputation: 493

Yes you can create a UITableview like view with a content view by creating a model UIView class and putting reported like tag in it .Import this model class to your UIViewController and manually create some delegate method to get number of view added and deleted .But the view should be created programmatically at run time

Upvotes: 1

A_Curious_developer
A_Curious_developer

Reputation: 493

you can create custom tableview cell using nib easily .Please check the link provided below. https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722

Upvotes: 0

Sandeep Ahuja
Sandeep Ahuja

Reputation: 931

We need to use UITableView subclass to customise appearance of UITableview cells. We can't do like you asked in objective c.

If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

Upvotes: 0

Related Questions