Restore default UITableViewCell backgroundView - ios

In short, I need to restore the default backgroundView for a UITableViewStyleGrouped cell.
cell.backgroundView = ??;
The long story:
I have a grouped tableview with some cells. For every cell the user needs to select some value or do some input. After that he can proceed to the next ViewController by touching a "next" button.
Anyway, to indicate to the user that he missed something, I display a nice red border around the cell with the wrong/missing user input. I do that by setting the backgroudView of the cell with a custom image like this:
cell.backgroundView = myErrorIndicatingImageView;
The cell now looks like this (screenshot, ignore the stars and label)
So far so good, this works like a charm. But after the user corrects the input I want to remove my red border background image and just show the original background again. The original background looks like this (screenshot):
And this seems to be a problem.
I tried several things
// try by setting the background to nil
cell.backgroundView = nil;
this removes the background completely and I'm lost with a cell without background.
// try it with addSubview and later remove the subview again
[cell.backgroundView addSubview:myErrorIndicatingImageView];
this does nothing. The myErrorIndicatingImageView is not visible. Even a [cell.backgroundView bringSubviewToFront:myErrorIndicatingImageView] does not help.
Right now the only solution I have is to create a new UITableViewCell for every single call to cellForRowAtIndexPath. This works but is just bad code and ignores completely the technique to reuse cells.
There must be a simpler solution...something like
cell.backgroundView = [self.tableView getDefaultBackgroundView];

what about trying this:
cell.backgroundView = [[UIView alloc] initWithFrame:cell.backgroundView.frame];
Or you can make the background view as a UIImageView and set 2 images in problem and fixed

Assuming you are doing this with a custom table cell class, save the original backgroundView in some ivar, then apply the new background. When you want to reset the background, set the backgroundView back to the original, saved background view.

Related

Swift 4 backgroundColor bug?

I have a problem with transparency in my Views, especially with UITableView.
Just to make sure. I already have 3 years of experience with swift and this is the first time, I am getting this problem.
So in apple's docs it says the following:
backgroundColor
The view's background color.
Changes to this property can be animated. The default value is nil, which results in a transparent background color."
But when applying clear color to backgroundColor or tableView.backgroundColor = UIColor.clear, the background is still appearing white.
Is this a bug or a feature?
EDIT:
The tableView isn't the problem anymore. It is the cell within the tableView.
Already tried solutions are clearing the background color of all subviews. Nothing changed.
EDIT 29.09.17
Ok, I debugged my application and got this:
UICachedDeviceWhiteColor and cachedColor.
A google searching also didn't help.
Edit 29.09.2017 Later that day ...
The problem occurs, when updating from swift 3 to swift 4.
I made a new project with swift 4, but it actually works fine.
Because it is a problem I have to solve. I will try some things and update this post from time to time.
In UITableViewCell and UICollectionViewCell there is a property view called contentView.
A common mistake is to manipulate the cell directly. Instead try to manipulate the cells content view:
Obj-c
UITableViewCell *cell = ....// get the cell
cell.backgroundColor = ... // BAD !
cell.contentView.backgroundColor = [UIColor redColor] // this is the correct way.
Swift
let cell = ...
cell.contentView.backgroundColor = UIColor.red // make sure its the contentView
also a couple small nuances with colors and opacity:
If you want only the background to have opacity (i.e. less than 100%) , make sure you are not changing the view's opacity (i.e. view.alpha = 0.5 BAD) - change the color's opacity value instead:
i.e.
cell.contentView.backgroundColor = [UIColor colorWithRed:... green:... blue:... alpha:0.5];
Another useful trick is to pause the application and press the "visual debug" button.
Search for your view and check it's attributes in the inspector
Found the problem:
When instantiating a variable which holds the view before displaying it, the background color of this view is white. The debugger calls this UICachedDeviceWhiteColor.
Why would you do that?
If you instantiate the view before it is displayed, you can switch with a menu between different views very fast with "bringSubview(toFront: ...).
How to fix:
To fix this you have to instantiate the view every time you want to show it.
Hope I could help some lost souls with this answer and Apple is going to fix this bug.
Verify that the view behind it is not white, then set the current view's (the one with the transparent background) modalPresentationStyle to overCurrentContext. This will allow views behind to show through the UIColor.clear background color.
If you are setting
tableView.backgroundColor = .red
and if it's an empty TableView then the Red will be visible. I give a headerView and FooterView also to visualise.
Now TableView Contents some data, so to reflect the color you need to set background color to cell.
class UserCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = .yellow
}
....
}
You can check out the original project here.

custom UIcollectionViewCell, using view to control color changes

I was going thru the tutorial here: http://pinkstone.co.uk/how-to-build-a-uicollectionview-in-ios-8/
And saw a part that looked great because it makes something simple, use of views to show color changes of selected/unselected items.-
(void)awakeFromNib {
// background color
UIView *bgView = [[UIView alloc]initWithFrame:self.bounds];
self.backgroundView = bgView;
self.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blue"]];
// selected background
UIView *selectedView = [[UIView alloc]initWithFrame:self.bounds];
self.selectedBackgroundView = selectedView;
self.selectedBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"pink"]];
}
The author showed that this is a simple approach that eliminates the need to manage the state of the cells.
Q. I want to change the color using this method during the highlight process. However, didHighlightItemAtIndexPath is in the CollectionViewController and I'd like to have it use the same process.
Basically what it does is change from one color to another based on selected/unselected. I'd like to use the add a color to indicate the in between state of being highlighted/unhighlighted.
I checked all the methods that are in cell that use UIView, and there is nothing for highlighted/unhighlighted.
Any ideas on an approach that has the advantages of using cell methods? Can I call a custom method from the viewcontroller method and load a view there?
You might think of this as a "press and hold" that changes the color when it's being held.
A UIView wouldn't know anything about it's selected or unselected state since it's a very elemental component and not specific to controls that allow selection. The UICollectionViewCell does have a selected property though, so that would be the right place to change these characteristics. Looking at the docs it seems like either tapping into setSelected or selectedBackgroundView would give you a good opportunity to customize the L&F of selected cells. There's also a highlighted state in case you might also be looking for that.

Change checkMark color in ios7, Interface Builder

Description
I am developing an application, using tableViews and selecting properties by putting a checkmark for each of selected items. It drove me crazy for 2 hours, because the code was correct, but I didn't see checkmark.
Problem
Actually the checkmark was being loaded correctly but I didn't notice, because the background was white and also the checkmark.
What I want to do
I want to change the color of the checkmark (possibly in Interface Builder) without using custom CheckMarks with different images on it.
Is that possible? If not possible why I am having white checkMark while the other one in another tableView is gray?
It's not possible if you are using accessoryType on cell.
If you want to be really flexible better create custom view instead:
UIImageView* tick = (UIImageView*) [cell.contentView viewWithTag:1];
if(tick == nil)
{
tick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Archive"]];
[cell.contentView addSubview:tick];
tick.tag = 1;
tick.frame = CGRectMake(285.0f, 20.0f, tick.frame.size.width, tick.frame.size.height);
}
With that you can do whatever you like, use custom images, add background etc...
If you want code to look nicer it will be better if you extend UITableViewCell and create custom class with some methods and properties to control that. I just gave you code to a quick way to achieve what you want.

scrollView with UIImage respond touch

I'm using a simple code to add some images to my UIScrollView. Also I've implemented another code to detect touches on each image.
Here is the code:
(void)handleSingleTap:(UIGestureRecognizer *)sender
{
int senderTagIs;
senderTagIs = sender.view.tag;
if (sender.view.layer.borderColor != [UIColor cyanColor].CGColor) {
sender.view.layer.borderColor = [UIColor cyanColor].CGColor;
UIImageView *showFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+100];
[showFullImage setTag:sender.view.tag+200];
[self.view addSubview:showFullImage];
showFullImage.hidden = NO;
NSLog(#"Show tag is: %i", sender.view.tag);
}
else
{
sender.view.layer.borderColor = [UIColor whiteColor].CGColor;
UIImageView *hideFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+200];
[hideFullImage setTag:sender.view.tag+100];
hideFullImage.hidden = YES;
NSLog(#"Hide tag is: %i", sender.view.tag);
}
}
The above code, sets the border color to cyan and show my small images from UIScrollView, in another UIImageView.
But my problem is, that I can't set the option to hide all images and set border color white for all images when one image is touched.
Ex: If I touch the first image, then the code will work, my big UIImageView will show touched image and touched image from UIScrollView will get the cyan color for border, so far so good.
Now, If I touch third image, my first image is shown, the color border is cyan, and so... I have to touch first image again to disable, but this is not what I want.
So, we've got a few things going on here. First, I'm assuming you're trying to show a collection of images in a scroll view with some custom padding to indicate selection around them. This sounds tailor make for using UICollectionView with a custom cell.
Absent further information, you're not resetting the old color. Either keep a reference to a selected image as a class variable or, assuming your image views are in a collection object like an NSArray, begin your method by iterating through the objects and resetting their view to an unselected state.
If you just need to hack together a solution, the second option should work. I really recommend using UICollectionView. It's a bit more work in the beginning, especially if you're not experienced with it, but it's well worth learning. Here's a good tutorial on UICollectionView.

Get rid of line above UITableView

I have a UITableView (which happens to have a UISearchBar) and can't seem to figure out how to get rid of the white/gray border above it. I need to have seamless black between the UISearchBar and the black above it.
I have tried hiding the UISearchBar to see if that had anything to do with it, but the line still appeared.
Any ideas?
In my case it helped to set:
searchBar.clipsToBounds = YES;
You have to customise the UISearchBar background to match according to your requirements.,take a look at this tutorial.
You should try this, by which you can set the color of cell borders and if you want to change the color of a particular cell's border put it in condition as: if (indexPath.row == yourcell):
tableView.separatorColor = [UIColor blackColor];
Also the above method you have to put in CellForRowAtIndexPath method of table view datasource.
Please notify if it works..
This had to do with the pullToRefresh view I was using (using SensibleTableView) - it had some code in drawRect to draw the line.

Resources