Custom Prototype Cell designed in Storyboard with Custom View Controller - ios

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).

Related

Replace a subview in UICollectionView, instead of changing subview properties

I'm trying to implement a subclass of UICollectionViewCell.
I'm getting an array of custom views, let's call it views passed from one object to another, also with an array called views. They are of type CustomView.
The UICollectionViewCell subclass has one property called view of type CustomView.
In CellForItemAt..., I'm trying to set the cell's view property from an array of CustomViews like this:
let customView = array[indexPath.row]
cell.view = customView
However, when I do this, the cells don't show properly; they're empty, and just grey shapes on the screen.
But, when I explicitly set the properties of the cell's CustomView subview, it shows how I'd like it to.
Is there a way to do a layout pass on the cell? Or do you have to just explicitly set properties in UICollectionViewCells instead of just passing in a pre-configured view?
The code is proprietary, so I have to be cryptic, but please let me know if I can clarify
Edit: Please see the comments in the accepted answer for further explanation
You should be adding constraints, or else you are getting the default auto resizing mask constraints. Call cell.setNeedsLayout() to force auto layout to recalculate before the next draw after you assing the voew.

Adding views to tableViewCell contentView on xib?

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.

How to make a UITableView look like this?

I'm curious of how Youtube `s UITableViewCells are done, here is a screenshot:
Like you can see the cells are like squared and centred, how can I do that?
While you actually can use UICollectionView instead of a UITableView, this layout (YouTube App) can actually be achieved with UITableView as well.
You will need to create a custom UITableViewCell, that has few images and labels. When you create a custom UITableViewCell, you add items to the contentView property. You can then change the constraints or frame of the contentView property to achieve the effect.
Of course, there are more ways to achieve this layout, but I would follow this one:
Create a custom subclass of UITableViewCell.
Give UITableViewCell a clear background.
Give white background to the contentView UIView property of the created subclass.
Add constraints for margin between cells to contentView in UITableViewCell.
Design the layout of the cell in contentView.
It is basically not hard to create a design like this.
Use a UICollectionView instead of a UITableView
Cells are automatically centered, just create a square size.

How to change Tableview's custom cell width dynamically ?

I have a table view and its every cell`s width say 1024px , so my requirement is after clicking a cell tableview's width will change logically. And my table view is bit complex.
Every cell contains a custom view which is defined in another class. Please help me..
I do not believe there is a way to modify a single cell's width without changing the width of the table view, only height using tableView:heightForRowAtIndexPath:.
Therefore, you might want to change the width of the custom view inside your cell.
First, to get the cell in question, call cellForRowAtIndexPath: on your table view wherever you need (if you want to change the width on tap, that would probably be in your tableView:didSelectRowAtIndexPath:).
To access your custom view, you may give your custom view a tag number inside of the UITableViewCell by setting the custom view's tag property when creating the cell or in the storyboard. Then, call viewWithTag: on the UITableViewCell instance to get the custom view, and modify the width of its frame property.
Another, perhaps more suitable option would be to use a custom UITableViewCell class for your table view cells. That would mean subclassing UITableViewCell and creating a property for your custom view, which would allow you to access the custom subview through the getter and then change its frame property.
If you need the width of the custom view's container to change (which is currently your cell), simply embed the custom view inside a UIView and modify the UIView's width rather than the UITableViewCell's instance in the same manner described above.

UITableViewCell customization, where to attach subviews?

when subclassing UITableViewCell to define a complete custom cell what style to set? UITableViewCellStyleDefault? Actually I don't need neither the label or the imageView, would they be allocated the same?
So when attaching my custom views to the UITableViewCell subclass, is it better to add to self, or to self.contentView?
And is it ok to do some quartz drawing in drawRect in the UITableViewCell subclass (Like background) ?
Thanks
when subclassing UITableViewCell to define a complete custom cell what style to set? UITableViewCellStyleDefault? Actually I don't need neither the label or the imageView, would they be allocated the same?
Since you are not using any of the pre-defined UILabel or UIImageView subviews of UITableViewCell, it doesn't really matter. I just use UITableViewCellStyleDefault. I do not know for sure if the UILabels or the UIImageView are allocated or not. However, since there is no way to explicitly tell the UITableViewCell not to create those subviews, I wouldn't worry about it. Even if they are created, you aren't using them so drawing the cell will not be any slower, and since cells are reused as you scroll through the table, allocating just a few extra objects once is not going to affect your overall table view performance.
So when attaching my custom views to the UITableViewCell subclass, is it better to add to self, or to self.contentView?
All of the content should be put in the UITableViewCell contentView. This will ensure that the contents behave properly when the UITableView has an index on the right or if the UITableView (or UITableViewCell) go into editing mode.
And is it ok to do some quartz drawing in drawRect in the UITableViewCell subclass (Like background) ?
The recommended approach is for the background to be placed in the UITableViewCell backgroundView property. So I would not recommend doing any drawing in the UITableViewCell subclass itself. Just use the contentView, backgroundView, and selectedBackgroundView properties. You can create a simple UIView subclass and just set that as the backgroundView of the UITableViewCell, if you like.
If you want to create a custom UITableViewCell, my experience is that it's much easier to use IB instead of code--even if you prefer using code normally. I would definitely recommend taking a look at these two links:
Creating a custom UITableViewCell in iOS 4
Custom UITableViewCell
This sounds like exactly what you're looking for -- you don't have to use the label or the image in this case, and you can make outlets to any custom UIViews that you need.
what style to set? UITableViewCellStyleDefault?
it depends on your aims. The most used style is UITableViewCellStyleDefault.
See Using Cell Objects in Predefined Styles
is it better to add to self, or to self.contentView?
developer.apple.com recommends to add subview to self.contentView (they specially call it as contentView).
is it ok to do some quartz drawing
It also depends on your aims. If you use only images or colors for background, then customize your cell view in IB, I don't see the reasons to use qurtz framework.

Resources