Update UIViewTableCell's background when accessory URL is clicked - iphone

In the tableView:accessoryButtonTappedForRowWithIndexPath: selector, I want to update the backgroundColor of the cell.
I can get the UIViewTableCell using cellForRowAtIndexPath: but I'm not sure how I can update its background color. The code beliw doesn't seem to work.
[_tableView beginUpdates];
cell.backgroundColor = [UIColor grayColor];
[_tableView endUpdates];
Any suggestions?

I would first try [cell setNeedsDisplay];.
Also beginUpdates and endUpdates is meant to be used when you are going to be inserted/deleting rows etc. so I don't think it's necessary here.
Background colors on cells is a tricky thing because the table view will do some magic behind the scenes to handle cell selection. To get reliable behaviour you need to set the cell's background color in this UITableViewDelegate method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

Related

How to update UITableViewCell drawing

My app displays some news in a UITableView. For each news-topic, the background color of the UITableViewCell is different (added a UIView in my custom cell).
So far, it works. But If I press on a UIBarButton to deselect one news-category, the content changes, but the background stays the same as before.
The color does not change, because the cell does not reload again after button click.
Any ideas?
Thank you!
You can set the color for the cell in (as per your logic):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
This will make sure the color is set every time the cell is displayed.
In your cellForRowAtIndexPath method, make sure that you're setting the color of the UITableViewCell.
Also, use [tableView reloadData];

Background image does not extend to accessory view background in table view cell

I'm using the code below to set a background under each cell in my table view. It works fine but there's a white space under the accessory view on the right side so the background does not cover this area. Any idea on how to fix this issue ?
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell-background.png"]];
}
Thx for helping,
Stephane
You probably want to create a custom cell. Please see Apple's documentation about contentView.
To add subviews to a cell to customize it furthur please see this. It also has a section on how to create custom cells from XIBs.

Add a view to a UITableViewCell's contentView when it is selected

I am trying to add a new view with 5 buttons to a cell once it is selected. I am trying to do this in 3 different ways. The first one is by checking if it is selected inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method.
if (cell == nil ) {
//initial customizaton of cells
} else {
if ([cell isSelected]) {
//I would add the view to the cell's contentView here but it never enters this if even though the cell is still blue, I scrolled the table back and forth several times and the blue cell never evaluated here
}
}
I have also tried to do it inside the following two methods:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
And:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I used tableView:cellForRowAtIndexPath: to get a reference to the cell and when I had that reference I tried adding the view inside the cell's contentView but it didn't seem to work(by "didn't work" I mean it didn't appear, I'll debug this further).
I know I didn't provide much detailed code but that's because I mainly want to know what's the general principle of how it should be done. I want to know why the cell doesn't stay with the selected property equal to YES even though it stays blue (when selectionStyle is the default one) when I tap it. I want it to stay "blue"(selected) with that view inside it until I deselect it and remove said view.
Did you try creating your own custom UITableViewCell? I think that is the point, you can overload setSelected: method there.
I followed NR4TR's advice and so far so good. I have subclassed UITableViewCell and have overridden the setSelected method in it.
Hoping this is useful to someone else in the future!

Drawing selected state on custom UITableViewCell

I have a customer UITableViewCell whose whole display is drawn indrawRect. When it draws it creates CGLayers so that it can reuse certain pieces when something is changed.
I have changed my UITableViewCellSelectionStyle to "None" because I don't want the default selected view to cover my drawing.
My problem is that I call setNeedsDisplay in setSelected:animated: for my cell but by the time drawRect is called, setSelected:animated: has already been called again to deselect the cell. In my table view controller didSelectRowAtIndexPath, I call deselectRowAtIndexPath as Apple advises.
EDIT - I have also tried called the setNeedsDisplay on my cell from my table view controller's (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath but that didn't change anything.
Any ideas? Thanks.
Use the table cell's selectedBackgroundView property. If you assign a custom view to that, it'll get shown and hidden at the same time as the default selection backgrounds—in other words, without having to wait for the setNeedsDisplay to get around to calling drawRect: on the cell itself.

How can set the background view of a UITableViewCell to the selected background when it is not selected?

I created a custom implementation of a multi-selectable table view. Multiple rows can be selected programatically. The only part I haven't been able to figure out is how to programatically set the background view of cell to the default system cell selected color/pattern.
Does anyone know how to do this?
Thanks!
Could you please post your method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Same code can help to understand the problem :)
Are you asking how to programatically select rows?
If so, then use the ‘selectRowAtIndexPath:animated:scrollPosition:‘ method call, or create the cell with
cell.selected = YES;
For the first cell, set it to selected as normal. For other cells, get the class of the backgroundView of the first selected cell, instantiate a new object of that class, and assign it as the background view.
otherCell.backgroundView = [[[[[firstCell backgroundView] class] alloc] init] autorelease];