I have a cell in a static table view which I want selected (highlighted blue) when the view appears. Does anyone know of a way to do this?
I have tried using cell.selected = YES;. This will make the cell selected for a very short time (it goes blue, then white just after).
I found the solution. I used cell.selected = YES; in viewDidLoad. Doing it in viewDidAppear will work.
I think you can write cell.selected = true in didSelectRowAtIndexPath also.
Related
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.
I have a table view with custom cells, each cell contains a textField. When the user tap on the textfield a UIPickerView will be displayed. I have this approach already done and work good. The thing is, the picker should display different data depending on which textField has been tapped, how can I detect this?
What I've done:
In viewDidLoad I create a view property which contains a UIPickerView.
In cellForRowAtIndexPath: I assign that view with the picker as an inputView for the textField (I did it this way to avoid creating a picker everytime a cell is rendered)
Ok, I found a solution that works really smooth:
I disabled the textField on every cell
I stop adding the picker as an inputView in cellForRowAtIndexPath
Once the user tap on didSelectRowAtIndexPath (which is called when they tap on the textField) I do the following:
if ([dataInfo[indexPath.row] isEqualToString:#"Accessibility"]) {
pickerItems = accessibilityItems;
}
else if ([dataInfo[indexPath.row] isEqualToString:#"Floor"]){
pickerItems = floorItems;
}
[picker reloadAllComponents];
SubCellTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.subcellSubtitleTextField setEnabled:YES];
cell.subcellSubtitleTextField.inputView = viewContainingPicker;
[cell.subcellSubtitleTextField becomeFirstResponder];
I hope this can help someone else!
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.
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.
I have a UITableView that draws a subView when the user touches a cell.
The problem is that the subView drawing is animated and if the user is fast enough they can tap a cell multiple times which I want to disable during the animation and afterwards.
I've tried using this:
- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath {
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
and also a bool variable:
if (isAnimating == NO) {
but neither seem to work. In each case rapid touches screws up everything.
Any help would be great thanks!
Try this...
After clicking on cell set
tableView.userInteractionEnabled = NO;
I know this is an old question however it lacked the swift version so,
in Swift 3 it is:
tableView.isUserInteractionEnabled = false;
and to turn it back on is:
tableView.isUserInteractionEnabled = true;
Just thought this might help someone if they were looking for the answer in swift 3 as I was, and ends up here.