UITableViewCell customization, where to attach subviews? - ios

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.

Related

Universal cell for UITableView and UICollectionView

I was wondering if there are some best practices for implementing universal cell which can be used with both of UITableView and UICollectionView...
I tried to put UITableViewCell into CollectionView but Xcode complained about this. I have just one variant left: make UIView which will implement cell's view and then implement UITableViewCell and UICollectionViewCell subclasses which will wrap this custom UIView so it could be placed into Table/Colleciton view. Is that the right way to achieve my goal?
Any thoughts on this?
Your idea can be implemented if the UIView's subviews are similar.You can make a template of UIView,create multiple widgets inside it,and create instance in UITableViewCell and UICollectionViewCell,as UITableViewCell and UICollectionViewCell are usually reused, you need to separate the data model and views,so realize your idea.

iOS alternative for Androids <include> tag?

How am I supposed to reuse views in iOS?
For example, I have a view that returns as a CollectionViewCell, inside of a UITableViewCell and on a specific spot in a UIViewController.
What is the recommended way to deal with this?
Do I make a xib and add a holder view everywhere I need it?
But, then to react to size changes, I need to add constraints in code, which seems to be too much hassle just for a simple inclusion of a xib.
Is there a feature in Xcode I can use?
Create an UICollectionViewCell in a XIB. Then this cell is placed inside an UICollectionView. You can place this UICollectionView basically everywhere, also inside an UITableViewCell. As the UICollectionView can be auto-layout constraint in the Storyboard the size changes automatically. No need for programmatic constraints. When you want to reuse the UICollectionViewCell just put it inside another UICollectionView in another UIViewController or UITableViewCell.
Btw: to use this just as "reusing"-feature you can disable the scrolling in the UICollectionView.

Custom-drawn UIView as UITableViewCell Image

We have a simple UITableView where we're using the default style since we just need a label and an image. However, we need our image to be custom-drawn.
We of course can't insert a UIView with a custom drawRect implementation as UITableViewCell expects a UIImageView, not a UIView.
We also tried subclassing UIImageView and overriding its drawRect but UIImageView doesn't call drawRect as its expecting its image data to come from an actual image, not a drawing routine.
Is there perhaps a way I can create a custom-drawn image and use that as the source for a UIImageView, which I can then simply pass to the UITableViewCell?
In lieu of that, the two (nasty) workarounds I've come up with are either
Perform custom drawing on the background view in UITableViewCell, or
Add our custom-drawn UIView to the view hierarchy of UITableViewCell's ContentView, bypassing using the built-in image capabilities.
Am I missing an easier way to achieve this?

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.

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

Resources