Make UITableview cell transparent when it get selected - uitableview

How to remove selection style color of UITableview while i select a particular cell ? When i change the selection style of cell to UITableViewCellSelectionStyleNone, the highlighted image doesn't appear in the cell. When i change that to UITableViewCellSelectionStyleBlue or UITableViewCellSelectionStyleGrey, the highlighted cell will distort will that particular gray or blue. My requirement is to make the cell with transparent background and when it gets selected, the selected image must show without selection style color.

I found the answer. Just put the below code inside cellForRowAtIndexpath method of willDisplayCell method
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
cell.selectedBackgroundView = backView;

For customizing the selected cell background and its controls state, you needs to override the following method..
- (void)setSelected:(BOOL)selected animated:(BOOL)animated

Related

Decrease Tableview selectable area in ios

1Hi in my project there is a requirement to show table row selection(blue color) only half of the row.Is there any way to do this.
The selected cell should be in blue color and remaining all are in white. Please help me to achive this also.I dont want to reload table because many images are there loading from server.If reload again again it will slow down performance.
[Please see the image after selection the cell should appear like this]
enter image description here
You can set selectedBackgroundView property of UITableViewCell with a view whose half is blue.
In didSelectRowAtIndexpath get cell ..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
// add view as per your according to need in cell
UIView * myCellAccessoryView = [UIview alloc] init];
// set frame of myCellAccessoryView as per your need ..
myCellAccessoryView.backGroundColor = // set color as you need .
// and add in cell
[cell.contentView addSubView :myCellAccessoryView];
}
hope it helps you ..
You can use this
UIView *backView = [[UIView alloc]init];
backView.frame = CGRectMake(0, 0, cell.frame.size.width/2, cell.frame.size.height);
backView.backgroundColor =[UIColor blueColor];
[cell addSubview:backView];
This will create a blue color background view.

Setting custom color for background of UITableViewCell accessory

I'm trying to get my table view cells to show a custom checkmark image when selected. The issue is that the color behind the checkmark image is gray instead of the custom cell color that I provide.
I'm aware this question has been asked before - I've read through and attempted to the following suggestions:
Wrong Background Color in Accessory View on iOS7
How to get Transparent Accessory View in UITableViewCell? (w/ Screenshot)
Altering the background color of cell.accessoryView and cell.editingAccessoryView
Among others. The general consensus seems to that changing the color of the cell's backgroundView will affect the accessory, but this does not seem to be the case for me. Here is my current code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[UIImageView alloc] initWithImage:_checkImage];
[self setCellColor:_customColor forCell:cell];
}
- (void)setCellColor:(UIColor *)color forCell:(UITableViewCell *)cell
{
cell.contentView.backgroundColor = color;
cell.backgroundView.backgroundColor = color;
cell.backgroundColor = color;
}
Note that my checkImage has a transparent background. Upon selecting a cell, the background of my cell changes to my customColor, but the background on the accessory remains gray. SOS!
The general consensus seems to that changing the color of the cell's backgroundView will affect the accessory
That is correct. The problem is that you are not doing that. You are saying this:
cell.backgroundView.backgroundColor = color;
But cell.backgroundView is nil, so that line is pointless. You need to give your cell a background view before you can set the color of that view.

Set specific Frame for Background Color by Cell click

i have the following Custom Cell. On the left is a blue label with an image on it, and on the right side a label with some text. No i want that the cell gets highlighted when i click on it, BUT only the white part behind the label should get highlighted, not the whole cell with the blue label and the image, any ideas?
I tried something like this:
categorieCell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, categorieCell.frame.size.width, categorieCell.frame.size.width)];
categorieCell.selectedBackgroundView.backgroundColor = [UIColor redColor];
If you don't want the cell to be highlighted as a result of the selection, just set the cell's selectionStyle property to UITableViewCellSelectionStyleNone. That will disable overall cell's highlight when it gets selected.
In tableView cellForRowAtIndexPath method apply the following code:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
After that you can change cell background upon the cell gets selected.
In tableView didSelectRowAtIndexPath method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *categorieCell = [tableView cellForRowAtIndexPath:indexPath];
categorieCell.backgroundColor = [UIColor redColor];
}
Don't forget to change cell background when it gets deselected in willDeselectRowAtIndexPath:

cell.selectedBackgroundView covering up UIViews in a cell

I have a table. Within each cell of a table, I created a simple bar graph (which is just 2 UIViews with background filled).
I have set cell.selectedBackgroundView to an image.
When the cell is selected, it seems to cover up parts of the bar graph. Does anyone know why?
The red is the selected cell. The top and bottom are unselected cells:
In the image, the grey bar, the brown bar, the 2 numbers (x.xx) and the semi-transparent line at the left, are all subviews of 1 UIView. The UIView is added to the cell. The line and the 2 numbers are still there, but the 2 bars are gone.
Here's some code:
Cell selected image set like this:
UIImage *selectedRowImage = [UIImage imageNamed:#"table-selectedcellbg-red-45px-stretch.png"];
cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[selectedRowImage resizableImageWithCapInsets:UIEdgeInsetsMake(selectedRowImage.size.height, selectedRowImage.size.width/2, selectedRowImage.size.height, selectedRowImage.size.width/2)]];
[cell sendSubviewToBack:cell.selectedBackgroundView]; // This didn't make a difference
Adding the view set like this:
UIView *graph = [barGraph getGraph];
UIView *graphView = [[UIView alloc]initWithFrame:CGRectMake(150, (tableView.rowHeight - graph.frame.size.height)/2, graph.frame.size.width, graph.frame.size.height)];
[graphView addSubview:graph];
[cell addSubview:graphView];
Any ideas?
I think the answer can be found in the UITableViewClass Reference:
selectedBackgroundView
...
Discussion
The default is nil for cells in plain-style tables
(UITableViewStylePlain) and non-nil for section-group tables
(UITableViewStyleGrouped). UITableViewCell adds the value of this
property as a subview only when the cell is selected. It adds the
selected background view as a subview directly above the background
view (backgroundView) if it is not nil, or behind all other views.
Calling setSelected:animated: causes the selected background view to
animate in and out with an alpha fade.
It seems as if selectedBackgroundView only works well if the corresponding backgroundView property of the cell is not nil.
Try adding a backgroundView and test again.
Hope that helps you out.
Edit:
Since this wasn't the solution, I tried to reproduce your problem, but with no success: the backgroundView stayed behind the QuestionMarkIcon (a view like your graphView)..
Code:
- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath {
//...
UIImage* selectedRowImage = [UIImage imageNamed: #"strecheableImage.png"];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage: [selectedRowImage resizableImageWithCapInsets: UIEdgeInsetsMake(selectedRowImage.size.height, selectedRowImage.size.width/2, selectedRowImage.size.height, selectedRowImage.size.width/2)]];
UIView* graph = [[QuestionMarkIcon alloc] init];
UIView* graphView = [[UIView alloc]initWithFrame: CGRectMake(150, (tableView.rowHeight - graph.frame.size.height)/2, graph.frame.size.width, graph.frame.size.height)];
[graphView addSubview: graph];
[cell addSubview: graphView];
// ...
}
This is how my stretchableImage.png looks like:
Cell 1 is selected but the QuestionMarkIcon (your graph) stays in front:
Sorry I couldn't help.

IOS uitableview cell selected background

I have a UItableview in my IOS app with some information in it. I changed the Selected background color to clearcolor using the following code:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
There is text and 2 images in the cell, I've build this using CGRectMake.
But When I select and hold a table cell the the images disappear behind what looks like a white background.
As you can see I'm holding the "Dacnusa sibrica" cell, how can I fix this?
If you want to disable the blue selection of cells you can also set the cell's selection mode instead of modifying the background:
cell.selectionStyle = UITableViewCellSelectionStyleNone
This simply disables the blue selection when tapping the cell but still allows the cell to be selected (and thus handled by code).
With Auto layout it is possible to define Selection=None (default is gray) in the Interface Builder.
have you used [cell.contentView addSubview:image]?
use [tableView deselectRowAtIndexPath:indexPath animated:YES];
for deselecting the cell

Resources