Opaque UILabel in a UICollectionViewCell has strange rendering behavior - ios

I've experienced some really weird behavior with UILabels in a UICollectionViewCell today and I'm hoping one of you guys can shed a light on this.
I can't show you code or full screenshots, but I will try to explain and illustrate it as well as possible;
I have a UICollectionView that has several cells and supports horizontal scrolling.
In the cells I have a label that I set on
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath and clear on - (void)prepareForReuse.
Seems pretty normal, and I even have an implementation exactly like this that works without any weirdness.
Now, for performance reasons, I set the UILabel's opaqueness ON in Interface Builder.
When I scroll around a couple of times, I get to see this:
Whenever I turned opaque OFF in Interface Builder, I get to see this:
I have a lot of experience with UITableViews and UICollectionViews (and reusing, etc) but there is no way I can explain this behavior properly...

yourLabel.backgroundColor = UIColor.whiteColor()
UILabel's default background color is clear color. If you set the label to opaque, you also need to set the background color to an opaque color.

Related

UITableViewCell background spanning multiple cells

This is end end result I want:
And this is the thing I tried initially.
This does not work, the cells below/above the cell with the background will overlap or underlap the background depending on when they are added into the tableview (like via dequeue/scrolling).
I am quite OK with this not working, and I believe I can achieve it by other means. For example by adding these backgrounds as views within the tableview itself and moving them based on the content offset or similar ways, maybe adding a background image that is tall with them embedded.
But. I am curious if there are some easier way, just adding the view into the XIB and applying a rotation would be very nice.
The background should be below the text in the other cells as well - this is where the complications comes in.
Anyway. Is this possible in some super-neat way?
What you should do is setting all cell's background to clear, and to set a background to your UITableView or your UIView.
Or, as you suggest, you can add a UIView with a rotation applied, and add it as a subview of your UIView/UITableView, and send it to back with [self.view sendSubviewToBack:backgroundView].

UISearchController search result cell background color

My UISearchController displays search results in UITableViewCells with background colour that I have not set
I can see by debugging the views that this grey colour is that of the cell.contentView. However, it doesn't react to me setting
cell.contentView.backgroundColor = [UIColor whiteColor];
in tableView:cellForRowAtIndexPath: method.
Are there any other methods to override that strange grey colour?
Are you able to provide an example project that reproduces your scenario? What does the tableView:cellForRowAtIndexPath: look like? If so, we can probably be much more helpful in determining the issue.
Without knowing specifics, it might be worth trying to set the contentView's backgroundColor in tableView:willDisplayCell:forRowAtIndexPath:, which occurs a bit later than tableView:cellForRowAtIndexPath:
It may also be worth making your own subclass of UITableViewCell so that you can better control the coloring of the cell.

How do I make a UITableViewCell separator show up?

I'm having a similar problem to this question: UITableViewCell separator not showing up.
However, the proposed solutions don't work for me. Specifically, I've checked the following:
My UITableViewCell subclass does not override drawRect:.
I don't have a custom UITableView and, therefore, I'm not overriding layoutSubviews.
In Interface Builder, I have the Separator attribute of the Table View set to "Single Line".
I've tried programmatically setting tableView.separatorStyle to UITableViewCellSeparatorStyleSingleLine.
But still the separator line will now show up… Does anyone know of something else that could cause this behavior or have any ideas for how I might track down what is hiding/removing the separator line?
EDIT:
Well I feel silly… In trying to create the screenshot requested by valheru, I noticed that the lines ARE there, but are almost exactly the same color as the background, so I couldn't see them until the screenshot was blown up. I had suspected earlier that the separator color might be the issue, and I tried testing for that by setting the separator color to white (my background is almost black), but it turns out that there was some copy/pasted code that was programmatically resetting the color to the almost-invisible color.
Thanks valheru for leading me to the answer. :-)
Check the height of your table view's row and the height of the cell. Make sure that the row's height is not less than the cell's height.

Twitter-like UITableView design

I'm trying to reproduce the design of Twitter UITableView (see image below) :
Did not manage to reproduce it :(
I tried several methods like :
UITableViewController with self.view.layer.borderWidth = 3.0f + shadows + cornerRadius
UITableViewController with Grouped style and corner Radius
UIView with UITableView as a subview and corner radius
Any clue to solve my problem please ?
In advance, thanks a lot for your help.
In my opinion, this seems to be NOT your classic grouped style UITableView.
This could be a UITableView (plain style) that spans across the whole device width, and composed by custom UITableViewCell's. The cells are narrow enough to leave room for the scroll indicators to be shown.
Each UITableViewCell renders a UIImage on the cell's background: each different, depending on position - top, middle, bottom - or content type. The image for the cell background would already contain the shadow pixels. In an experience of mine, using Quartz for shadow, is too processor intensive and slow, iphone 4). In addition, each custom cell could also have its view setOpaque:TRUE for enhanced performance (against the solid colored background).
Each tweet cell may span different lines/heights so you'd probably use [UIColor colorWithPatternImage:[UIImage imageNamed:#"tile.png"]] when setting the background of the cell.
So we end up with no real shadow / processor intensive stuff. Just graphics, color matching and positioning.
This very similar to twitter UI
https://github.com/vicpenap/PrettyKit
I have used a simple trick wherein you use a UIView "Vbg" under UITableView and set the colour of cells and Table to clearcolor. Then increase the height:
Vbg.heightConstraint.constant = number of cells * height of cell.
I had to use this for fixed height cells , so this trick came up my mind and it worked pretty well.

UITableView row count like iOS Mail app

How do you add counts inside of a UITableView UITableViewCell like the iOS Mail app?
In addition to DDBadgeViewCell (mentioned by #micpringle), there's also TDBadgedCell.
I tried out both and found TDBadgedCell to suit my needs more, as it puts the badges over the cell's text rather than under it, meaning the badges are visible even for cells with long texts.
The project also seems to be (currently, at least) more active than DDBadgeViewCell. (That being said, there seems to be a bug in the non-ARC version of TDBadgedCell.)
Create a custom UITableViewCell, position the labels where you want them (title, subtitle, count, whatever you need). I highly recommend Matt Gallaghers custom UITableView code - it takes a lot of the headaches out of dealing with custom rows. You'll have to follow Matt Gallaghers steps for customizing the cell.
To get the appearance of the count label as close as possible to your example (mail.app), you'll have to set the UILabel backgroundColor to gray (or whatever color you want it to be), textColor to white, and layer.cornerRadius to something equal to half the height of the label (if label is 20 high, cornerRadius should be 10). This will result in a UILabel with white text, gray background, round corners. Note - this isn't the most efficient method of doing this, but Apple hasn't put up the WWDC session video where they explain the performant method better (I missed that session).
The easiest solution would be to set an UILabel as accessoryView or using a custom UITableViewCell subclass which could be designed using IB.
I'd recommend creating a simple rounded UIView and a UILabel as a subview in it. I'd probably create a UITableViewCell subclass to manage the content.
Definitively the most easy way would be using a ready-to-use class like TDBadgedCell

Resources