Unable to fetch UITableView Footer - ios

I'm trying to change the background color of the Footer of a UITableView section. However, I'm not having any luck.
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor redColor];
cell.detailTextLabel.textColor = [UIColor redColor];
UITableViewHeaderFooterView *header = [_tableView footerViewForSection:i];
[header setBackgroundColor:[UIColor redColor]];
The first part - where I set the colors in the cell - is working. The second part - where I want to set the footer color - isn't. What am I doing wrong?

You need to set the color inside the cellForRowAtIndexPath method and inside the footerViewForSection method.
The table view will request cells, headers and footers as it needs them. For cells, chances are it will reuse the same cell which is why you cell color seems to work. However if you do some scrolling you will find it probably stops working.
Your header view does not work because you are asking for a view in your code, but that will not be the same view the table uses as it will have asked for its own.
So you should set the colors when the cell, header or footer is being requested.
Ideally the cell/header/footer configurations should be driven from your data model. e.g. Inside cellForRowAtIndexPath, you should create the cell appropriate for the data model entry for the index path. Sets its color based on the color required by the data model entry. All changes then are done on the data model and you reload the affected sections and/or rows.

Is -footerViewForSection: returning a value? My guess is it's returning nil. Are you providing the footer view to the table view in the delegate methods? If you're not, it doesn't exist. If you are, you can set the color then.

Related

UITableViewCell will not work with UIColor colorWithRed [duplicate]

I have created a bunch of cells which I reuse in a table view. All of those cells just have different UILabels inside them and some UIImageViews (nothing covers the complete cell).
Setting the background color in IB has no effect (always white or transparent, can't say which one of them). But if I press Command-R (simulate interface) the cell has the correct background color in the simulator.
I tried to set it in tableView:cellForRowAtIndexPath: but it doesn't work like I would think either.
This does the trick:
cell.contentView.backgroundColor = [UIColor redColor];
but these have no effect (even if I set the cell.contentView.backgroundColor to clearColor):
cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];
I set all the layout/font/background stuff in IB. Any idea why this isn't working in this case?
Why do I need to modify the contentView's backgroundColor and not the backgroundView's?
It seems to be a common issue. Could somebody please point me in the right direction to (finally) understand how background colors are handled within a table view cell.
To change the background color of the cell, you have to do it in the willDisplayCell:forRowAtIndexPath: method. This is mentioned in the UITableViewCell documentation near the bottom of the Overview. So you need:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
The contentView is just the recommended subview to put custom controls in so that the cell gets layed out properly when table editing is going on.
There's an easier solution: When you create a UITableViewCell in Interface Builder, simply drag and drop an additional UIView so that it covers the entire UITableViewCell you're creating within IB. Place your additional UI elements on top of this additional UIView. You can set this extra UIView to whatever background you like.
There are quite a few answers on StackOverflow that suggest you change the background color of your UITableViewCell by using a line of code like this one:
cell.contentView.backgroundColor = [UIColor redColor];
If you add a drop shadow to your cell by adjusting the contentView's frame size slightly, you may find that this line will change both the background color of your cell and the color of your drop shadow area as well.

Keep the color of text's cell when come back to table

I changed the color of text for the cell clicked in the table. But after the cell is clicked, when i come back to table the text of cell has the original color. Could you give me an advice?
This is the code in "didSelectRowAtIndexPath"
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.highlightedTextColor = [UIColor blueColor];
Thank you
after the cell is clicked, when i come back to table the text of cell has the original color. Could you give me an advice?
You need to have the color for each cell stored somewhere other than in the table, so that you can reproduce the colors you want anytime the table redraws itself. Typically, you'll have some sort of data structure that stores the table's data, and that's usually the right place to save any changes the user makes. The table view's data source should have a -tableView:cellForRowAtIndexPath: method that sets the color according to what you've saved, along with any other cell attributes.
This is happen because the cells are reused, so lets say when you change text colour property of some cell it will be affected as you expect but when you scroll and that cell disappear off the screen it will be put to reuse pool and if it appears again on the screen table view takes some cell from the reuse pool but it's properties will be different so the colour won't persist.
You should keep somewhere, for example in NSMutableArray, info about which table was clicked.
You can add an index path to the array when you click the cell and in cellForRowAtIndexPath: check is this indexPath in the array and if it is change appropriate property.
The problem is that iOS throws away your cell if you scroll away and recreates it when it's needed (you scroll back to the cell).
If I were you, I would subclass UITableViewCell and overwrite
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
In there you would have
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected: selected animated: animated];
self.textLabel.textColor = selected ? [UIColor blueColor] : [UIColor blackColor];
}
Since iOS UITableView remembers which cell is selected, this should work fine, even when it's recreated.
The reason it's happening is what others are saying: cells are reused.
Storing selection state or color will work, however if you just need to make sure that selected cells have a different color for a label than non-selected cells, there's a way that does not require to use a supporting data structure.
You just need to check if the cell being setup at - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is currently selected or not, and that can be achieved with [tableView indexPathForSelectedRow] if your table uses single selection, or [tableView indexPathsForSelectedRows] if it uses multiple selection.
The last case requires you to find the current indexPath in the returned array, and might be slower than using the supporting array.
But if the selection is simple, then this solution is probably faster, uses less memory and is easier to read (IMO).

UITableView with scrolling background pattern

I have a UITableView with the backgroundColor property set like so:
tableview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"IMAGE"]];
This gives the tableview a background, that scrolls, which is exactly what I want.
The problem is, when I add cells to the tableview, in each cell the background pattern of the tableview starts new / starts over:
In the above image, you can see the problem in the red circled areas.
For the tableview cells, I have set all backgroundColors to clearColor for
backgroundView
textLabel
detailedTextlabel
contentView
I have also tried to set
cell.backgroundView = [UIView new];
cell.backgroundView.backgroundColor = [UIColor clearColor];
and the same for the contentView, too.
Additional Note:
Setting the backgroundView of the tableview does not help, since then the background does NOT scroll anymore!
Just ran into this issue and found this fixes the cell having it's own copy of the backgroundColor
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
}
Here is what I would do:
1) Create a view controller and set the view background color to your pattern image.
2) Add your table view controller as a child view controller to the newly created view controller. This can be done in code or if you are using iOS 6 and storyboards, with an embed segue. If you are doing it in code you may need to set the frame of the table view controller manually.
3) Set the background color of your table view controller to [UIColor clearColor]
I had a similar issue and this is what worked for me.

UITableView has cells, but shows no cells

So, the correct number of rows shows up. On pressing any row, the correct action takes place. However, the cells themselves are nowhere to be seen.
I added a NSTimer in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for each cell just to trace it out beyond the function - they all say that their superview is equal to the tableview in question (and is not nil, so i'm not checking nil == nil or something).
They all contain labels with the correct text.
The separator lines are being drawn.... If I change the TableView background, the whole visible area's background shows as that color.
I'm checking that each cell is neither hidden nor set to an alpha of 0.
Is there anything else I could be missing?
Are you loading from your cells from a nib file or creating programmatically?
Are you overlaying another object over your cell in the cell subview? Perhaps a subview is covering it; I can't tell, since you have not posted any code yet. Given the information you have provided, it is difficult to determine why you cannot see the cells backgroundView.
Try changing the color with
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
tmpView.backgroundColor = [UIColor blackColor];
myCell.backgroundView = tmpView;
It sounds like you have set the backgroundView of your cell to [UIColor clearColor].

UILabel background color not updating in UITableView

I'm having a perplexing problem with a simple UILabel I put on a cell in a UITableView. I enter a separate view after tapping on a row, like many UITableViews. In there, I update the cell so that when I return to the rows, it should be updated, with this:
MyTableViewCell* cell =
(MyTableViewCell*) [mTableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:mActiveRow inSection:0]];
cell.myLabel.text = #"New Value"; // updated text
cell.myLabel.backgroundColor = [UIColor greenColor]; // updated color
When I return, though, only the text is updated, not the color. When I scroll the row off the screen and return it refreshes properly via another path of code that has exactly the same code. Could there be some trigger to refresh specifically the background color of the UILabel? I'm not sure why the text will refresh but the color won't.
Neeeeeeevermind. I had overlooked the [TableView reloadData] method to refresh the cells. That worked perfectly.

Resources