UITableViewCell labels opaque=YES looks bad when clicking cell - iphone

I want my table cells to load fast, so I am setting all my UILabels inside my cell to be opaque=YES; This is fine, because I also set the backgrounds to white and it looks normal.
The problem comes when you click the cell, since the backgrounds of those labels are white, the blue selected color looks pretty bad when trying to highlight the cell. Is there a work around for this? Would setting the background color of those cells to clearColor just defeat the purpose of setting opaque?

There is something you have to consider. First, setting the labels to opaque is definitely the right way of getting good scrolling performance.
The proper way to do this is declaring a subclass of UITableViewCell and overwrite the setBackgroundColor method like this and forward the background color to each element of the cell:
- (void) setBackgroundColor:(UIColor *)color {
[super setBackgroundColor:color];
[titleLabel setBackgroundColor:color];
[imageView setBackgroundColor:color];
[timeLabel setBackgroundColor:color];
}
I used this as the file's Owner of the XIB where the tableview cell is defined and have connected the UI elements to outlets in this custom subclass.

Related

Subclassing UITableViewCell for grouped table

I'm trying to create a a custom subclass of a UITableViewCell to be used in a grouped table.
I'm laying out the subclass with a nib. (I've just tried doing it without the nib but getting the same problem).
Whenever I've done this before I've wanted to create a whole new cell style with a different background etc... so in awakeFromNib I do this...
- (void)awakeFromNib
{
// N.B. I am not doing this, this is how I normally get rid of border
// in this case I want the border so I am not running this code.
self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
self.backgroundColor = [UIColor clearColor];
}
but in this case I want to keep the background with the rounded corners and I'm just adding different UI elements to it.
The cell gets created and all the elements are laid out correctly but I'm getting an annoying "second border" in the top left corner of every section.
I have a screen shot showing this.
At first I thought this was a hair on my screen or something but it isn't.
I've tried setting the backgroundView and backgroundColor but that removes the normal cell background and I want to keep them.
Any ideas how I can get rid of this?
EDIT
Just to clarify what I said above about setting backgroundColor and backgroundView.
Here is what happens when I change them...
In this I set the backgroundView to nil. The border remains but so does the little bit I'm trying to get rid of.
If I set the backgroundView to a new UIView then this happens...
I want the same background with the white background and the rounded corners border. Just not with that annoying little bit.

Edit mode for custom UITableViewCell with drawRect

I have a UITableViewCell subclass with this drawRect:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (self.checked)
{
[[UIImage imageNamed:#"checkMark.png"] drawInRect:CGRectMake([self.contentView bounds].origin.x + 10, 12.0, 22, 22)];
}
}
The problem occurs when the cell goes into edit mode and the drawn image doesn't move to the right like the UITextField and accessory views do. Instead the image becomes hidden by the delete button. How can I get the drawn image to animate to the right?
Thanks for your help.
You need your cell to redraw itself when entering editing mode. Usually all it will do is resize the content view, which won't cause your checkmark to be redrawn.
However, to animate this is a little harder since you are directly drawing the check mark. You could try directly drawing it in the content view instead, or adding a subview to the content view which contains the checkmark - this will get moved automatically.
You could even use the built in image view, it seems to be in approximately the right position?

UITableViewCell Background Color?

I have done a fair bit of googling on this and I can't find the right solution. I have a UITableView of which I want to change the colour of the background of the cells. I have found solutions to doing this but it only deals with cells which have content or data as it were.
I need to colour the background of those unused cells as well.
Try setting the background colour of the tableView itself. Those "empty cells" at the bottom aren't really cells at all - they're just separator lines drawn over the background.
Even though UITableViewCell inherits from UIView, changing the backgroundColor property of the cell itself won't do anything. You need to change the background color of the cell's contentView, such as
cell.contentView.backgroundColor = [UIColor greenColor];
This is because the subviews of a UITableViewCell are actually subviews of the cell's contentView, because the contentView knows how to resize its subviews if a cell is put into editing mode; the cell itself doesn't know how to do that.
I'm not sure what you mean by unused cells. If you tell your tableView there are 10 cells and you only provide content for 8 of them, you'll still have 10 green cells, if that's what you mean by "unused".

Different colors for grouped tableView and tableViewCell backgrounds

How can I set different colors for GROUPED UITableView inside background vs. background, which is outside my table? That is the part which is visible when tableView is smaller than the screen or scrolled beyond limits of table.
UPDATE: that sample image is generated by my current code! I DO NOT NEED TIPS HOW TO MAKE IT !!! Please read the question before you (try to) answer. Would really appreciate this, but thanx anyway.
File AboutViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundView = [[CustomView alloc]
initWithFrame:self.tableView.bounds]; // VISIBLE
self.tableView.backgroundColor = [UIColor redColor]; // NOT VISIBLE
}
In the sample image (generated by my current code) the background view contains sample gradient from yellow to green, each cell "bubble" background is texture, each label has own background - all this just to demonstrate what I could define. As you can see, background gradient is partially visible through each cell and remains static when you scroll the table.
What I want to do: I want DIFFERENT tableView background for GROUPED UITableView.
For plain UITableView style I have defined a custom background for each cell, but it doesn't seem to work with GROUPED tableView. This only sets the cell background inside the cell "bubble". How to define the cell area outside that cell "bubble"?
For this you can use the image as backgroundimage.
But I think you are using the grouped table,so
you need to go with the tableview background color
and set that to image color.
Like
winTableView.backgroundColor=[UIColor colorWithPatternImage:#"bg.png"];
Or you can even go with the Gradient layer,inserting that in you view.
Hope this will help you.
Get the image of different color as you want to show the background of your table view.
For this u set cell.backgroundColor =[UIColor redColor]; its work
and for background u tale image or set color
For cell color or view you can also use these.
Try to have different color for different cells by your logic.
cell.backgroundColor=[UIColor clearColor];
//cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.accessoryType=UITableViewCellAccessoryNone;
UIView *backroundSelecView=[[[UIView alloc]init]autorelease];
backroundSelecView.backgroundColor=[UIColor colorWithRed:203.0/255.0f green:218.0/255.0f blue:140.0/255.0f alpha:.80];
cell.selectedBackgroundView=backroundSelecView;
cell.backgroundView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"table-grid#2x.png"]]autorelease];

Custom UITableViewCell with drawRect + normal blue selection

I have a custom UITableViewCell. All the drawing is done in the drawRect: method and no subviews are added.
The drawing works fine and the scroll speed is good.
But the problem is with the selected cells. I want the selection to behave like it would normally:
Blue gradient selection color
Inverted text color
Animated deselection
I have not been able to achieve all three.
First attempt:
Set selectionStyle = UITableViewCellSelectionStyleNone and do the selection color in the drawRect method.
With this method, I am able to achieve the first two things, but the deselection is instant. So one second it is selected blue, the next second it is deselected. I do not get the nice fade-out.
Second attempt:
selectionStyle = UITableViewCellSelectionStyleBlue
With this method my cell is all gradient blue when selected. The text I have is not visible. The fade-out works nicely, though.
Third attempt:
Set selectionStyle = UITableViewCellSelectionStyleBlue and also set selectedBackgroundView to a UIView where I set the backgroundColor to a transparent blue.
Here the problem is that the selectedBackgroundView (despite the name) is drawn on top of my normal content. So if the selectedBackgroundView is not transparent, I cannot see the text. And if it is transparent, I can see the text, but the selection color gets "faded" and does not look right.
What would be the correct way of achieving all three bullet points?
OK. So the answer to my own question... Don't subclass UITableViewCell for custom drawing. Instead subclass a UIImageView or a UIView.
This post has a nice description of how to do it:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
I'd suggest using the second attempt in combination with a gesture recognizer. Check if the user presses on a cell, if so, set the textcolor in that cell to yellow (or what is the inversion of blue? xD).
Sample code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view isKindOfClass:[UIButton class]]) {
//do something with that view, for example:
//change the color of the text in that view,
//or invert it ;)
return YES;
}
return NO;
}
Alternatively, you can attach a UIGestureRecognizer to that button. That way, you get the class called inserted in the selector.
There might be a better option to do what you asked. This just came up my mind, if you have 2/3 points working already, this should help you to get 3/3.