Adding views to tableViewCell contentView on xib? - ios

I have created a xib file that is setup as a custom UITableViewCell class. This xib is simple and just has a UILabel inside of it with constraints binding it to the superview. I am utilizing this tutorial to implement dynamic cell height. Everything should be working, but by using a xib all of the views I add are not added as subviews of the content view, but the cell itself. This is creating an issue with autolayout. The dynamic resizing is dependent upon using the contentView of the cell to determine the required height.
How can I add a view to a xib that will be a subview of the cells contentView and not the cell itself?

This ended up being an overlook on my part. I had created the cell using the default view that xib's are initially setup with. I needed to drag out a UITableViewCell view from the object library and add my labels etc. to the content view of that cell.

Related

UITableView edit mode appears on top of my views

edit mode problem
As you can see in the picture, when I enter the edit mode the delete button appears on top of all the views in the cell.
I assume this is because I am using programmatic UI with autolayouts not the storyboard and some of the constraints are preventing actions, but I cannot find out how I can resolve this issue.
I suspect that when creating your UITableViewCell subclass, you did not add your custom subviews to the cell's contentView.
From the documentation:
The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode.
Sounds like your problem exactly.

Unwanted UIView over UICollectionViewCell (TapGesture Doesn't work )

Recently, I see a strange thing in Xcode 11.4.
When I create a UICollectionView with Its cell, adding TapGesture doesn't work. By debugging on its view in runtime, I noticed a view covers all the cells. It seems it is ContainerView.
This view prevents users to click or tap on items.
Any help is appreciated
Are you adding your subviews and tap gesture to the cell's contentView?
In the documentation for UICollectionViewCell (https://developer.apple.com/documentation/uikit/uicollectionviewcell), it says:
To configure the appearance of your cell, add the views needed to
present the data item’s content as subviews to the view in the
contentView property. Do not directly add subviews to the cell itself.
The same applies for UITableViewCell as well.

How to add a subview to a UIView's contentView in a UIStoryboard? (iOS)

I have a UITableView that I've created in a UIStoryboard. It contains one Prototype UITableViewCell. The cells are populated in the ViewController's cellForRowAtIndexPath: method.
I'm now trying to add the ability to delete cells using commitEditingStyle:forRowAtIndexPath:. Everything is working except that when the delete button animates over the cell, the rest of the content doesn't shift over and it looks really ugly. According to this SO question, content will only be shifted if it's added to the cell's contentView rather than to the cell itself.
As such, is there a way to add a UIView (Ex. a UIButton) to a UITableViewCell's contentView within a UIStoryboard?

Custom Prototype Cell designed in Storyboard with Custom View Controller

The main problem is that it does not respect the design done in the storyboard. The coordinates of the image and the labels are as a basic cell. Note that such attributes are observed as the alignment of the label.
The button that has been added, is below the label.
I've definitely seen this issue before and I believe it is because imageView and textLabel are properties of UITableViewCell and the default implementation of layoutSubviews is overriding your storyboard's layout with UITableViewCell's defaults.
Try overriding layoutSubviews, or provide your own image and label properties (named differently from UITableViewCell's, ie: myImageView and myTextLabel).

UITableView inside UICollectionView handling touches.

Collection view consists of a single row, of horizontally aligned cells, which size is the same size as the collection view's bounds, single cell fills entire screen.
The problem is that the collection view seems to be intercepting all of the pans. How can I forward them to the table so I can also scroll the table vertically.
I want vertical pan to be delivered to the table inside the cell, so it can scroll up and down. I want horizontal pan to be delivered to the collection view, so it can scroll horizontally.
Any ideas? Thanks.
For UITableView inside CollectionView using storyBoard, please follow these steps:
-Drag CollectionView to UIViewController, drag datasource to UIViewController(don't drag delegate). Add datasource methods inside ViewController.m
-Create Cell:CollectionViewCell class. choose class for Cell in storyBoard to Cell class,specify reuse ID.
-Drag tableView inside collectionCell square. delegate, datasource drag to CollecionCell Square too. Add tableView datasource, delegate inside Cell.m
-Create CellDelegate when implement tableViewDidSelect inside it. Transfer this delegate to UIViewController to perform other action
Sample code: https://github.com/lequysang/github_zip/blob/master/TableViewInCollection.zip
Swift2 update - We don't need to do all the above steps. Just making the controller the delegate and datasource of both the tableview and collection view works just fine.
I've been able to make this work without using storyboards--although, I use XIBs for my views but only to add the Autolayout rules. All customisation (colors, fonts, labels, etc.) are set in code. In any case, this should also work for cells that are being built programatically.
How to do it:
The most important thing to remember is to add your custom view to the contentView property of the cell and not inside the cell itself. If you are using XIBs for the custom UICollectionViewCell, leave it blank. Instantiate your custom view (where the UITableView is), then add it as a subview of the UICollectionViewCell's contentView. If you add the table vie directly inside the collection view cell, the table view will not vertically scroll.
Implement shouldSelectItemAtIndexPath and shouldHighlightItemAtIndexPath of the UICollectionViewDelegate and make both return false, so that tapping on the collection view cell does not intercept with taps intended for the table view inside.
Done with Xcode 7.3 for iOS 9+.

Resources