UITableViewCell selected row's text color change - ios

I am having a tableview and I want to know that how can I change the selected row's text color, say to Red ? I tried by this code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [localArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cityName = [localArray objectAtIndex:indexPath.row];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.textColor = [UIColor redColor];
//theCell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
(1) When I select any row then text color changed to the red but when I select another then previously selected row's text remains red. how can I solve this ?
(2) When I scroll the table text color change to the black color how to solve this ?
Thanks..

Do this in tableView:cellForRowAtIndexPath::
cell.textLabel.highlightedTextColor = [UIColor redColor];
(And don't use cell.text = ... anymore. It has been deprecated for almost 2 years now. Use cell.textLabel.text = ... instead.)
As Raphael Oliveira mentioned in the comments, if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone this won't work. Check Storyboard as well for the selection style.

If you want to change the text colour only, with out changing cell background colour. you can use this.Write this code in in cellForRowAtIndexPath method
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];

I had the same problem, try this!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (id object in cell.superview.subviews) {
if ([object isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cellNotSelected = (UITableViewCell*)object;
cellNotSelected.textLabel.textColor = [UIColor blackColor];
}
}
cell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
That may be the solution to your (and mine) problem.

If you're already subclassing UITableViewCell, it's easier/cleaner to set the colors in the awakeFromNib method (assuming you're instantiating from a storyboard or xib).
#implementation MySubclassTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
self.customLabel.highlightedTextColor = [UIColor whiteColor];
}
#end

Related

UITableViewCell cannot be set transparent entirely with check box

I use below code to try to make tableview cell background clear colour when selected and it works without checkbox but when there is it's not clear colour totally.
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
As you can see from below picture, it's only clear under checkbox but not entire cell. But when there's no checkbox (not editing mode) it's entirely clear. How can I fix this? Thanks in advance.
If I remove the above code, the result will look like this. The white background will cover entire cell. I don't know why the above code can just set clear background to the area beneath checkbox?
add this method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Need to set clear background of cell and cell'contentView.
how about setting the selection style to none instead of blue?
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I have find solution, We can edit below code
- (void)viewDidLoad {
[super viewDidLoad];
//Add below to line
[self.tblContact setAllowsMultipleSelectionDuringEditing:YES];
[self.tblContact setEditing:YES animated:YES];
//tblContact is tableView Outlet
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//ContactTableViewCell is xib file of UITableViewCell
static NSString *identifier = #"ContactTableViewCell";
ContactTableViewCell *cell = (ContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
cell.backgroundColor = [UIColor clearColor];
return cell;
}

How do I programatically change UITableViewCells text color at runtime

I want to enable a "dark mode" feature to my app and in my tableview cells, I'm changing textcolors like so
if ([FS isDarkModeEnabled]) {
cell.CATEGORY.textColor = [UIColor whiteColor];
cell.ORIGINUSER.textColor = [UIColor whiteColor];
cell.NUMFILES.textColor = [UIColor whiteColor];
cell.NUMTASKS.textColor = [UIColor whiteColor];
cell.SHARECOUNT.textColor = [UIColor whiteColor];
}
I figured I'd replace the above code with a for loop but
for (id obj in [cell.contentView subviews]) {
if ([obj isKindOfClass:[UILabel class]]) {
NSLog(#"%#", ((UILabel *)obj).text);//<-- this spits out my storyboard placeholder text and not the acutal text for some reason.
[((UILabel *)obj) setTextColor:[UIColor greenColor]];
}
}
and
FSCategoriesTVCCell *cell2 = [self.tableView cellForRowAtIndexPath:indexPath];
for (UILabel *lbl in [cell2.contentView subviews]) {
[lbl setTextColor:[UIColor greenColor]];
}
but am unable to get it working.
All the above code is done in - tableView:cellForRowAtIndexPath:
edit to add some clarification
I know how to change the text colors manually, what I want to do is loop through the cell's content and change the colors via a for loop and NOT by declaring each label and changing each one individually.
Or, in shorter words, I want a for loop, not to do cell.Label1.textColor = blah
When the dark mode is enabled by the user, you have to inform the tableview that it should update the cells. To do this, simply call tableView's [tableView reloadData] method.
Option 1:When View Load
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.text = #"highboi";
return cell;
}
Option 2:When Click or Select the tableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor redColor];
}

UITableViewCell background color when highlighted/selected

I want my cell text color to change when tapped, but not the background color.
I want the cell background to always be white, and only the text color to change when selected.
I've seen a bunch of answers about how to do this...
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor whiteColor];
[cell setSelectedBackgroundView:bgColorView];
... but the view that gets created runs on top of the cell separator.
And to use cell.textLabel.highlightedTextColor = [UIColor brownColor]; to change the textColor, I can't have cell.selectionStyle = UITableViewCellSelectionStyleNone;, so I need to figure out something.
if you want to show the separator of cell , you may need this:
add this code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.textLabel.highlightedTextColor = [UIColor brownColor];
UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, tableView.bounds.size.width, [self tableView:tableView heightForRowAtIndexPath:indexPath] - 2)];
backgroudView.backgroundColor = [UIColor whiteColor];
UIView *placeholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
placeholderView.backgroundColor = [UIColor clearColor];
[placeholderView addSubview:backgroudView];
cell.selectedBackgroundView = placeholderView;
...
}
// These codes are used to show the separatorView when the cell didSelected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){
for (id obj in cell.subviews) {
if([obj isKindOfClass:NSClassFromString(#"_UITableViewCellSeparatorView")]){
UIView *view = (UIView *)obj;
view.hidden = NO;
}
}
};
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
showSeparatorView(cell);
if (indexPath.row > 0)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
showSeparatorView(cell);
}
...
}
// These codes are used to show the separatorView when the cell didHightlight
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){
for (id obj in cell.subviews) {
if([obj isKindOfClass:NSClassFromString(#"_UITableViewCellSeparatorView")]){
UIView *view = (UIView *)obj;
view.hidden = NO;
}
}
};
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
showSeparatorView(cell);
if (indexPath.row > 0)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
showSeparatorView(cell);
}
}
Hope these can help to you!
Add this in cellForRowAtIndexPath method so when you tap in cell than change label text color
cell.textLabel.highlightedTextColor = [UIColor brownColor];
You can use this -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
[c.textLabel setTextColor:[UIColor brownColor]];
}
You can add the code cell.selectedBackgroundView = [UIView new]; to figure out this. With this method, you don't need cell.selectionStyle = UITableViewCellSelectionStyleNone;.

Change the border color of view in tableview

I have a UIView in UITableView and I need to change the border color of the UIView in a selected row.
How can I do this?
In UITableView cellForRowIndexPath I declared a UIView named as polygonView.
In UITableView didSelectRowAtIndexPath method I wrote code to change the border color.
I also need my row to be unselected when I select a different row.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 40, 10, 300, 43)];
//polygonView.backgroundColor = [UIColor blackColor];
UIImage *image1=[UIImage imageNamed:#"rectangle_box (2)"];
polygonView.backgroundColor = [UIColor colorWithPatternImage:image1];
polygonView.tag=indexPath.row;
cell.textLabel.text=[names objectAtIndex:indexPath.row];
[cell.contentView addSubview:polygonView];
[cell.contentView addSubview:lbl[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIColor *color = [UIColor redColor];
polygonView .layer.shadowColor = [color CGColor];
polygonView.layer.shadowRadius = 1.0f;
polygonView .layer.shadowOpacity = 1;
polygonView .layer.shadowOffset = CGSizeZero;
}
There is a didDeselectRowAtIndexPath... delegate method you can implement which gets called when you select a different row. From that delegate method you can access the cell (using cell = tableView.cellForRowAtIndexPath:deselectedIndexPath) and the polygon to change the border color.

Subclassed UItableViewCell selection

I have subclassed a UITableViewCell so that I can increase the scrolling performance which has worked out great for me so far.
In my subclass I have a method called seSelected that looks like this
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
self.backgroundColor = [UIColor lightGrayColor];
}else{
self.backgroundColor = [UIColor whiteColor];
}
}
I would like to know how to make it so that if I touch the same cell it deselects the cell and changes the color back to white? I have tried a few different if statments in setSelected but nothings working.
Use this delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and call here method which manage selected and unselected cell.
One approach could be to set the tag on the cell and then use it to set the background color.
typedef enum {
CellSelected = 0,
CellDeselected
} CellSelection;
While creating the cell set the tag to "CellDeselected"
cell.tag = CellDeselected;
And then when cell is tapped just check which color you want to set in the background.
switch (customCell.tag) {
case CellSelected:
self.backgroundColor = [UIColor lightGrayColor];
break;
case CellDeselected:
self.backgroundColor = [UIColor whiteColor];
break;
default:
break;
}
customCell.tag = !customCell.tag;
Do like this...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
str = [YourArray objectAtIndex:indexPath.row];//str is a string variable
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIndentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [reminder objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor whiteColor];
if ([str isEqualToString:[reminder objectAtIndex:indexPath.row]])
{
cell.backgroundColor = [UIColor grayColor];
}
return cell;
}
You can use for this for your custom cell.....
use UITableViewCell method: [cell setSelectedBackgroundView:myBgColorView];.
Apple Documentation.
Example:
UIView *myBgColorView = [[UIView alloc] init];
myBgColorView.backgroundColor = [UIColor greenColor];
[cell setSelectedBackgroundView:myBgColorView];
set cell style:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and your code will work. But, you set color only selected cell.
If you need set color when cell is pressed, override this method:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

Resources