Kousik
Kousik

Reputation: 22425

How to add a custom UIView on top of the UITableViewCell runtime?

I have a TableView having multiple custom cell. I want to add custom View on top of the cell runtime.

I have cell like this.

enter image description here

Now I want to add the HR Channel top view on the same cell runtime so that the it should look like this.

enter image description here As example :

Think about facebook feed. It has a TableView with custom cells like Text Post Cell, Image Post Cell,Group Post Cell,Event cell, Page Post cell (Post which is posted in page). Now facebook add a custom view on top of the cell if someone has liked, comment or tagged on the post.

How can I add the custom view on top of the cell without creating one more custom cell?

Upvotes: 1

Views: 286

Answers (2)

EricXuan
EricXuan

Reputation: 387

You can use an UIStackView to hold the entire custom cell, including the HR channel part. And you can set the HR channel part hide first if needed, and set no hide in the code in the runtime. The UIStackView will automatic change the cell's view when the HR channel hide or show.

Holp it useful!

Upvotes: 1

Pushkraj Lanjekar
Pushkraj Lanjekar

Reputation: 2294

Do one thing design your cell as per this https://i.sstatic.net/1LZI8.png

Keep Icon, HR channel label and right icon on one view. Give height to it using constraints. Make IBOutlet of its height like

IBOutlet NSLayoutConstraint *layoutConstraintHeightTopView;

Then in cellForRowAtIndexPathcheck your condition whether to show it or not and change height constant as per that like

(isTopViewToShow == true) ? cell.layoutConstraintHeightTopView.constant = 50 : cell.layoutConstraintHeightTopView.constant = 0;

and also in heightForRowAtIndexPath check same condition and give heigh like

(isTopViewToShow == true) ? return 250 : return 200;

Hope this will help you !!!

Upvotes: 1

Related Questions