Edit UITableViewCell property on scrolling - objective-c

I am making an app in which there are quotes from famous people in an array which are being read out by openears and a tableView which shows all the quotes. The currently reading quote is to be displayed in the tableview in a different color than the other quotes. I tried with the following function in cellForRowAtIndexPath but the color of the cell remains the same unless the user scrolls it out and then come back to the view. But I need to set the color automatically and at the same time all other cells should revert back to the initial(white) color. Is there any method which can achieve this? I don't want to reload the table each time to achieve this.
if ([[arrayOfQuoteIds objectAtIndex:indexPath.row]intValue] == currentQuoteId) {
cell.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.contentView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.textLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.detailTextLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.accessoryView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
}
else {
cell.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = [UIColor whiteColor];
cell.accessoryView.backgroundColor = [UIColor whiteColor];
}

I can be wrong but how You want to achieve change of cell appearence without redrawing cell?
If You don't want reload all table try to reload just changed rows
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Related

Custom cell in UITableView not working properly in iOS 13 Dark Mode

I have used, since 2011, Objective-C code for my medical application, which basically has 5 tabs with associated UITableViews, 3 of which use custom cells. Initially with using Dark Mode I found that the custom cells did not change to Dark Mode automatically. Instead, I needed to implement the code below to test for Dark Mode and make cell text and background changes accordingly.
The problem is that the Dark Mode changes do not occur for empty cells in the first tab, above or below the filled cells; they remain blank white. However, similar, empty cells in the UITableViews associated with the second 2 tabs DO behave as expected in Dark Mode.
The sample images below show the cells displayed by the first and third tabs. I'm wondering at this point if there is a bug in the OS, or whether I just am not implementing the proper change, to explain why Dark Mode does not work properly in the first tab only.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier ";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.calculationLabel.text = [dictionary objectForKey:#"Title"];
[cell.calculationLabel setLineBreakMode:NSLineBreakByWordWrapping];
cell.calculationLabel.numberOfLines = 0;
//Make color changes in cells relevant to Dark mode, if present.
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { //Dark mode
cell.calculationLabel.textColor = [UIColor whiteColor];
cell.calculationLabel.backgroundColor = [UIColor blackColor];
cell.statusLabel.backgroundColor = [UIColor blackColor];
cell.favoriteStatus.backgroundColor = [UIColor blackColor];
cell.backgroundColor = [UIColor blackColor];
} else { //Light mode
cell.calculationLabel.textColor = [UIColor blackColor];
cell.calculationLabel.backgroundColor = [UIColor whiteColor];
cell.statusLabel.backgroundColor = [UIColor whiteColor];
cell.favoriteStatus.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor whiteColor];
}
cell.statusLabel.textColor = [UIColor systemGreenColor];
You need to be using the proper colors, the ones that automatically adapt between light and dark mode.
Avoid colors like whiteColor and blackColor. Use labelColor and systemBackgroundColor. Use those colors for the cells and the table view. Then you don't need to have any code that checks the current traits or interface style.
Code such as:
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { //Dark mode
cell.calculationLabel.textColor = [UIColor whiteColor];
cell.calculationLabel.backgroundColor = [UIColor blackColor];
cell.statusLabel.backgroundColor = [UIColor blackColor];
cell.favoriteStatus.backgroundColor = [UIColor blackColor];
cell.backgroundColor = [UIColor blackColor];
} else { //Light mode
cell.calculationLabel.textColor = [UIColor blackColor];
cell.calculationLabel.backgroundColor = [UIColor whiteColor];
cell.statusLabel.backgroundColor = [UIColor whiteColor];
cell.favoriteStatus.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor whiteColor];
}
becomes:
cell.calculationLabel.textColor = UIColor.labelColor;
cell.calculationLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.statusLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.favoriteStatus.backgroundColor = UIColor.systemBackgroundColor;
cell.backgroundColor = UIColor.systemBackgroundColor;

Background colour of UICellView Changing

I have a table full of cells, and have changed the background colour by doing the following:
//set the backgorund color for the cells
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:0.5 green:0.3 blue:0.5 alpha:0.5];
//bg.backgroundColor = [UIColor redColor];
cell.backgroundView = bg;
cell.textLabel.backgroundColor = bg.backgroundColor;
The problem is that the background colour of the cells changes when you scroll them out of view, and then back into view.
Before:
After dragging the menu up, then back down:
I've tried to do some troubleshooting on my own and realized that this doesn't happen when I use one of the predefined UIColor , for example [UIColor redColor]
Take a look here.
Set the BackgroundColor property:
cell.backgroundColor = [UIColor greenColor];
in:
tableView:willDisplayCell:forRowAtIndexPath:

custom tableview and highlightedTextColor

i have a table view, with custom cell, i have set the cell for have a highlighted color on text when you tap on it.
//cell specific
NSString *ligneTableau = [NSString stringWithFormat:#"%#", [[table objectAtIndex:indexPath.row] nome]];
cell.label.text = ligneTableau;
cell.label.font = [UIFont fontWithName:#"populaire" size:35];
cell.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
cell.fondo.image = [UIImage imageNamed: #"cell_ant.png"];
//highlighted Text
cell.label.highlightedTextColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
every things work fine, but when come back to the table the text stay highlighted.
I forget somethings?
Probably the cell remains selected when you go back to table. So as soon as come back to table you should set deselected.
[cell setSelected:NO];
In your UpdatesTableViewCell class, you can implement the following methods:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted)
self.label.textColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
else
self.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected)
self.label.textColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
else
self.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
}

Changing elements in selected UITableViewCell subclass

I have implemented a custom UITableViewCell for my application and have created a custom background view and selectedbackgroundview, both of which work great. However, I have several other images and uilabels on the cell that I want to change the color's of when it is selected. I have overwritten the following method and it logs correctly when the cell is selected but it does not change the image or the text color.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
NSLog(#"setSelected:YES");
separatorImage.image = [UIImage imageNamed:#"SeparatorSelected"];
titleLabel.textColor = [UIColor whiteColor];
} else {
NSLog(#"setSelected:NO");
separatorImage.image = [UIImage imageNamed:#"Separator"];
titleLabel.textColor = [UIColor grayColor];
}
}
Any Idea what I am doing wrong?
Update
separatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Separator"]]];
[separatorImage setFrame:CGRectMake(99, 4, 5, 71)];
separatorImage.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:separatorImage];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(111, 4, 188, 26)];
titleLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:21.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:titleLabel];
I have a hunch that sending -setSelected:animated: to super at the beginning of the method is preventing the changes to your cell. Try moving the call to super to the end of the method. You should also override -setHighlighted:animated: or your selection will appear to lag.

How to customize the background color of the standard label within a UITableViewCell?

I'm trying for the past several hours to figure out how to do this, but with no luck. There are several potential solutions for this when searching Google, but nothing seems to work.
I'm trying to customize the background color of the standard UILabel that goes in a UITableViewCell (since I already customized the background color of the cell itself), but nothing I do seems to work.
I'm creating my own UILabel to customize the colors in -tableView:cellForRowAtIndexPath:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
cell.text = #"Sample text here";
But that doesn't work, and the resulting table view still has a bunch of cells with labels with black text and white background in it.
Any clues on what I am doing wrong here?
UPDATE: If I try to do the following instead:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
label.text = #"Sample text here";
I get a bunch of UITableViewCells with no text at all.
It appears that you're assigning the text to the cell instead of the label. You probably want:
label.text = #"Sample text here";
Also, you'll need to set the label's frame to what you require:
label.frame = CGRectMake(10,10, 80, 40);
or directly in the constructor:
label = [[UILabel alloc] initWithFrame:myFrame];
I wouldn't do this in code. I would do it with a custom XIB file and then load it in your
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate function. That way you can design the XIB file to your exact specs, and tweak it.
Good luck!
You have asked this question before, and received correct answers;
How to customize the background color of a UITableViewCell?