Select row and change button color - ios

In My tableview, cell is custom cell and I have button and label inside it.
lable and button background color is Green.
When I click on Particular row then Button color change and label background color is as it is. Here I used following code to set label background color via layer,
cell.lblCountMsg.backgroundColor = [UIColor clearColor];
cell.lblCountMsg.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0].CGColor;
but, same code is not working for Button. i tried with layer also
cell.btnTeting.backgroundColor = [UIColor clearColor];
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
but not set green color and also changed the color of selection color.
See, My Screen Shot (Breaking new) is Green.
Please help me to find solution.
Thanks in Advance.

You should implement didSelectRowAtIndexPath something like,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Here your custom cell's class instead of UITableViewCell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.btn.backgroundColor = [UIColor redColor];
}

If you made a UITableViewCell subclass, try overriding the
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
method. In this method you can change your views in the cell when the cell is highlighted.
So for example:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
self.btnTeting.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];
}
UPDATED
#interface SuggestedGroupCell ()
#property (strong, nonatomic) IBOutlet UIButton *btnSubScribe;
#end
#implementation SuggestedGroupCell
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
self.btnSubScribe.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];
}
#end

If you want to change background color of button,remove this line and then try:
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
But if you want to change button title color you can write code like this:
[cell.btnMorePupils setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
Here UIControlState you can take what ever like UIControlStateNormal, UIControlStateSelected etc as per your requirement.

Related

Add transparent overlay as subview of `uiimageview`

I have a UITable with custom cells, each of which has a uiimage. When I highlight the cell, the image is not getting highlighted.
To highlight the cell's image, when I select a cell in my table, didHighlightRowAtIndexPath is called. In that method, I change the background color of the cell. I also call a method, indicatedImageViewPressed, which I got from this post, to add an overlay to the image of the cell.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Add your Colour.
SpeciesCell* speciesCell = (SpeciesCell*)[tableView cellForRowAtIndexPath:indexPath];
speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
[self indicatedImageViewPressed:speciesCell.imageView on:YES];
}
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.frame];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000]; // Your color overlay goes here
[imageView addSubview:overlay];
[imageView bringSubviewToFront:overlay];
}
overlay.hidden = !on;
}
However, the cell's image does not change. No overlay is added. I tried bringSubviewToFront to make sure the overlay wasn't obscured by some other view, but it still doesn't work.
UPDATE: I am able to add a border width/color to speciesCell.speciesImage
speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
replace this with below code:
SpeciesCell.selectionStyle=UITableViewCellSelectionStyleGray;

background for uitableview cell

MyTableCell *cell=[[MyTableCell alloc]init];
cell.backgroundColor = [UIColor clearColor];
The above lines of code are still showing the white color of the cell's background and hiding the background image of the tableview.
Go to you storyboard and set the color of your uitableview cell to clear color from there.And also set uitabelview color also to clear color.
Or in you viewdidload set tableview background color to clear color and in cellforrow at indexparth delagate set
cell.contentView.backGroundColor = [UIColor ClearColor]
try this
- (void)tableView:(UITableView *)tableView willDisplayCell(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
set the contentview background color also
cell.contentView.backGroundColor = [UIColor ClearColor];
cell.backGroundView.backGroundColor = [UIColor ClearColor];
cell.selectedBackGroundView.backGroundColor = [UIColor ClearColor];
Also check if you cleared the tableView.backGroundColor also
Thank you every one. I have solved the problem. I changed the background attribute in the attribute inspector in the storyboard for my cell object from default to clear color and it worked.
You can do it this way
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.backgroundColor = [UIColor clearColor];

Change UILabel textColor when UITableViewCell is highlighted?

I have a custom UITableViewCell, with an UILabel and an UIImageView. I want to change the background color and the text color when the cell is highlighted. In my CustomCell's setHighlighted method, I have the following piece of code:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(self) {
if(highlighted) {
self.title.textColor = [UIColor whiteColor];
} else {
self.title.textColor = [UIColor blackColor];
}
//highlight background
UIView *bgColorView = [[UIView alloc] initWithFrame:self.frame];
bgColorView.backgroundColor = [UIColor blackColor];
[self setSelectedBackgroundView:bgColorView];
}
}
I already tried to put the code for textColor change in the tableView's didSelectRowAtIndexPath, but it's not the effect that I want - I want the text color to change when the user touches down on the cell - not at touch up.
Any suggestions?
You should use the attribute highlightedTextColor. Set the color for the cell inside tableView:cellForRowAtIndexPath and it should look like this:
cell.textLabel.highlightedTextColor = [UIColor blueColor];
try this
[cell.textLabel setHighlightedTextColor:[UIColor yellowColor]]
If you have custom label then use following code
lblPrd.highlightedTextColor = [UIColor whiteColor];
override method
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
//TODO set view highlighted color
}
in custom tableviewcell
In case you have a Storyboard or XIB, you can simply set this at Attributes Inspector > Label > Highlighted.

UITableView Cells background color NOT changing?

I have a UITableView set up in two storyboards - one for iPhone and one for iPad.
I have set it up in the iPhone storyboard such that the cells have a clear background and so a background image shows through - this works perfectly fine.
HOWEVER, when I copy this view in to the iPad storyboard, for some reason the table cells have a white background even though all the settings are the same (clear colour still set in the builder).
What could be the reason for this?
EDIT - I've looked at this further and if I change the colour of a row to anything, it won't change - for some reason it remains white :/
Appears to be a known issue with this, and the fix is by adding the following to the .m file for your class:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
}
You have to set the backgroundColor of the cell's contentView to your color
cell.contentView.backgroundColor = [UIColor yourcolor];
From iOS 7, UITableViewCell have a default white background.
Set: cell.contentView.backgroundColor = [UIColor clearColor];
and eventually : cell.contentView.backgroundView = nil;
And verifiy also your:
tableview.backgroundColor = [UIColor clearColor];
tableview.backgroundView = nil;
extension UITableViewCell {
func setTransparent() {
let bgView: UIView = UIView()
bgView.backgroundColor = UIColor.clear
self.backgroundView = bgView
self.backgroundColor = UIColor.clear
self.layer.backgroundColor = UIColor.clear.cgColor
self.contentView.backgroundColor = UIColor.clear
}
}
and call that method in cellForRowAt

UITableView weird separator line color

i have a problem in setting UITableViewCell background color. I wanna change it into my own color, so I use this code to change that background color :
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [ColorManager backgroundInput];
bg.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bg];
I implement this code in CellForRowAtIndexPath method.
The weird thing I have is, when i tap into a cell, the separator line color being highlight just like the image below. I just wanna make it still dark, anybody have idea?
Thank you
After setting up your own background view,which will have the line or not,
you can clear the default tableview color
tablename.separatorColor = [UIColor clearColor];
OR
If you don't want to remove the default line then you can give same color of background view to line color
//You can specify the RGB of background view
tablename.separatorColor = [UIColor colorWithRed:R green:G blue:B alpha:1.0f];
I think the best option is
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Another option is to set the backgroundColor of the UITableViewCell to a clear color.
cell.backgroundColor = [UIColor clearColor];
try it
Objective-c
tablename.separatorColor = [UIColor blackColor];
Swift
tablename.separatorColor = UIColor.blackColor();

Resources