UITableViewCell with black background - ios

I know this may be obvious, but i didn't set the background to any color in IB. My code for the UITableView is this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.textLabel.text = [_listeDesChapitres objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
What I get is this:
The cell background is black by default, until I click it to see its text. Am i missing something?

You need to set the background of the label, and the background of the cell:
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];

use :
cell.contentView.backgroundColor=[UIColor clearColor];
instead of:
cell.backgroundColor=[UIColor clearColor];

Related

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];
}

Cell text colour

I am somehow having problems with something as simple as setting text colour with
-(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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.textColor=[UIColor lightTextColor];
cell.backgroundColor=[UIColor clearColor];
}
NSArray *array=[[NSArray alloc]initWithArray:messagingArrayToBeDisplayedInTableWhenCalloutTapped[indexPath.row]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.textColor=[UIColor lightTextColor];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[array objectAtIndex:0];
cell.detailTextLabel.text=[array objectAtIndex:1];
return cell;
}
The background colour works, but setting text colour to whiteColor has no effect so both the title and subtitle are a default grey colour. I have tried changing colours in my prototype cell, but that also did not work. Is anyone able to suggest a solution? Thanks in advance :)
dequeueReusableCellWithIdentifier:forIndexPath: always returns a valid cell, there fore cell is never nil and that code is never executed.
So remove that condition and see if it works.

How to get the UITAbleViewCell highlighted state to change the cell background?

I'm trying to change the cell background image when user tap on a cell (highlighted state), I've been trying this way but it's not working:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.textLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"] highlightedImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
}
- (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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"]];
cell.textLabel.text = [self.listOfMenuSettings objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"settings_icon_%d", indexPath.row]];
UIImageView *pressed = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
[cell setSelectedBackgroundView:pressed];
return cell;
}
What I'm missing?
I don't find setSelectedBackgroundView: instance method in apple documentation. But there is a property selectedBackgroundView.So try:
cell.selectedBackgroundView = pressed;

How do I change the font color in a tableview cell to a variable UIColor?

I am adding the color here but it is not reflecting in the cell.
- (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] autorelease];
}
cell.textLabel.font =[UIFont systemFontOfSize:14];
//cell.textLabel.textColor = theFontColor;//this does not work
//cell.textLabel.textColor = [UIColor whiteColor];//this works
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
EDIT
The comfigureCell was overriding the color. Sorry was a real noob error.
Have you defined somewhere theFontColor as a UIColor
You should do for example:
UIColor *theFontColor = [UIColor colorWithRed:64.0f/255.0f green:58.0f/255.0f blue:43.0f/255.0f alpha:1.0f];
...and then use it

UITableViewCell selected row's text color change

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

Resources