Changing background of UITableViewCells in Edit Mode - ios

I have trouble with a black table view appearing white when swiping left to delete (in my case void) a row.
I tried setting every possible parameter for color in every step all the way from the ViewController down to the Content of the TableViewCell.
I managed to get it darkGray for "selection" with the following in CellForRowAtIndex:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor darkGrayColor];
[cell setSelectedBackgroundView:bgColorView];
So I figure the solution is something like that.
I have a similar setup on iPhone where the TableView is in a TableViewController where the issue does not occur. In this particular situation the TableView is in a regular ViewController.
Screenshots attached. Please advise. Edit: Note that the white space is not a button that appears next to the Void-button. It's white space that changes size according to how far your drag the cell. If you release the cell the white space disappears entirely, but the bounce reveals the white space.
Swiped left (in motion):
Swiped left (released):
Unswiped:

Wow - it turns out I only needed to implement the following method:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor blackColor];
}
Ironically I searched high and low for a solution, only to find it 15 minutes after I ask. It's been bugging me for weeks.
Ah, well, maybe someone else stumbles across this problem.

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.

Prevent dragging of cell to the left in iOS7 tableview editing mode

Screenshot below shows one tableview cell in editing mode after swiping to the left on the row. While in editing mode I can hold the cell and drag further to the left again which reveals this white space (bouncy effect when I let go). I'm setting each cell's background image as follows:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CellBG.jpg"]];
I have 2 questions.
Is it possible to prevent this extra dragging so this white area will never be seen? Would like the same behaviour as the iOS7 weather app for example.
Also, any idea why there is thin white line under the delete button?
Thanks
Is it possible to prevent this extra dragging so this white area
will never be seen?
Yes. Put the image on the UITableView's background. Not on the cell. Because if you put the image background on a cell and when you horizontally swap a cell to delete, that cell will move. That is to say the image background will also move. But if you put it on the UITableView, it will not move with the swap. Remember to set UITableView and UITableViewCell's background color to be clear color.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseIdentifier"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cellBackground.png"]];
imageView.frame = CGRectMake(0, kCellHeight*indexPath.row, 320, cell.frame.size.height);
[tableView addSubview:imageView];
[tableView sendSubviewToBack:imageView];
return cell;
}
2.Also, any idea why there is thin white line under the delete button?
I think that is Apple's little bug. As you can also find it in the weather app. So never mind that.

Background Texture of a grouped UITableView won't scroll with the table

I'm working on an app that has lots of UITableViews and I'm trying to give them a textured background color. I need to use the Grouped style because I don't want the section headers to float over the text fields when the user scrolls.
The problem I'm having is that when I use the Grouped style, the background texture doesn't scroll with the table; it stays in place while the table scrolls above it. I feel like this is kind of weird and I would rather have the background scroll with the table, the way it does in the Plain style. Unfortunately, because I can't have the header views floating on top of everything, that doesn't appear to be an option.
Has anyone been able to accomplish this?
Here's some relevant code:
- (void)loadView {
[super loadView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texturedPattern.png"]];
// this prevents the cells from replicating the background texture
self.tableView.backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// disable the default grouped border since we're doing it manually with the textField
cell.backgroundView = nil;
cell.backgroundColor = [UIColor clearColor];
}
And my table view currently looks like this:
UPDATE
As Amit Vyawahare suggested, I tried applying the background texture to the background of the headers and each cell. There are two problems that are both much more obvious when you see it in motion, but I'll do my best.
First, the background is always visible. I've removed the background color from the table to make it more obvious:
Everywhere you see black, the background texture of the tableView would be visible and it will not scroll with the tableView. The Grouped tableView style inserts the 5 pixel border on either side of every cell and can't be changed. Additionally, there is no footer beneath the Staff ID section, and I've even implemented -tableView:heightForFooterInSection: to return 0.0, but there's still a gap there.
Second, even if I were able to get rid of these gaps, the textures don't line up anyway. Again, this is difficult to see, so I've uploaded a retina screen shot to make it a little easier:
This is most obvious above the Password section, you can see the textures don't align properly and it looks kind of like a "fold" in the paper. Which would be cool, I guess, if that's what the client wanted. It's visible, but less obvious on just about every edge from the second screen shot. This is because the texture is actually quite large: 200x200 (400x400#2x), and there are slight variations in color that aren't noticeable unless this sort of misalignment happens.
First replace your UITableViewController by a UIViewController and add a UITableView to it. Set the autoresizingMask to flexible width/height. Now you have something equivalent to a UITableViewController, but with more control over the view hierarchy.
Then add a view below the tableview (actually: add that one first) which holds the background.
Finally, set the delegate of the scrollview to your class, and respond to scroll events by updating your background view accordingly.

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].

Cell background incorrectly repeating in UITableViewCell when scrolling

I have a full list that populates a UITableView. This I want to background the one of the cells with a different color and it works initially, but for some reason when I start scrolling the table up and down, it starts drawing more cells with the green background.
Please note that there is always one detailCell.detailTimeLabel.text that's equal to currentTime.
The code I have is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... SOME MORE CODE HERE ...
if ([detailCell.detailTimeLabel.text isEqualToString:currentTime]) {
UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backgroundView.backgroundColor = [UIColor greenColor];
detailCell.backgroundView = backgroundView;
for (UIView* view in detailCell.contentView.subviews)
{
view.backgroundColor = [UIColor clearColor];
}
}
}
Which can be the problem?
Your trying to store data in a tableviewcell. You can't do this because the cells are constantly reused. The background you see repeated occurs because its the same cell being displayed over and over again with different text.
When you dequeue the cell, you need to reset it to blank and wipe out all the previous data. Then you should set the background color only if the data you are putting into the cell has the current time.
As a general rule, you should never refer to data in the cells. If you need to know what is in a particular cell, look at the data at the indexpath within the datamodel itself.

Resources