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?
Related
Say I have a UICollectionViewCell (applies to any view however) with a UIImageView (again, any view) and I'm not using auto-layout. I want to apply a CAGradientLayer on top. Inside the awakeFromNib I could use the frame.size of the image view to create a bezier path for my shadow. This works great!
The problem is if I use auto-layout constraints to size and position that image view inside the collection view cell. Constraints aren't applied at the awakeFromNib callback point. So I can't use the first approach. My solution thus far has been to defer that CALayer positioning to layoutSubviews. This is not an optimal solution because it is called multiple times and you can actually see the CALayer reposition itself.
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.
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).
I have to draw two horizontal lines in a cell, one in the vicinity of the edge at the top and the other near the button one. These two lines should be very pale yellow, that is the same color as the background view.
I must to create a subclass of UIView or a subclass of UITableViewCell?
You can subclass a UIView, create custom DrawRect code inside it and load it as the cells background view. Here is an excellent tutorial on how to do this.
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.