Alternate between 3 colors in UITableviewCell - ios

I have a UITableView that I would like to display in 3 different background colors. Basically white, light gray, dark gray, then back to white and so on.
This is all I have been able to find on stack overflow so far:
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [UIColor lightGrayColor];
} else {
cell.contentView.backgroundColor = [UIColor darkGrayColor];
}
This works perfect for 2 colors. How can I alternate between 3 colors?

Just use 3 instead of 2 and then set the values accordingly, e.g.:
NSInteger colorIndex = indexPath.row % 3;
switch (colorIndex) {
case 0:
cell.contentView.backgroundColor = [UIColor whiteColor];
break;
case 1:
cell.contentView.backgroundColor = [UIColor lightGrayColor];
break;
case 2:
cell.contentView.backgroundColor = [UIColor darkGrayColor];
break;
}
You can also use [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0] with whatever numeric values you want if you want a more shades of gray (e.g. 50).

Rob is correct but i will use default color provided by UIColor:
NSInteger colorIndex = indexPath.row % 3;
switch (colorIndex) {
case 0:
cell.contentView.backgroundColor = [UIColor whiteColor];
break;
case 1:
cell.contentView.backgroundColor = [UIColor lightGrayColor];
break;
case 2:
cell.contentView.backgroundColor = [UIColor darkGrayColor];
break;
}

Try this:
if(indexPath.row % 3 ==0){
cell.contentView.backgroundColor = [UIColor whiteColor];
}
if((indexPath.row-1) % 3 ==0){
cell.contentView.backgroundColor = [UIColor lightGrayColor];
}
if((indexPath.row - 2) % 3 ==0){
cell.contentView.backgroundColor = [UIColor darkGrayColor];
}

Related

Where to set UITableViewCell other than in cellForRowAtIndexPath ? Objective-C

In a wish to improve UITableView performance, I'm thinking of relocating all of my UITableViewCell's settings outside cellForRowAtIndexPath.
At this moment, I have several custom cells inside my tableview that I set inside cellForRowAtIndexPath. But because of this, everytime I scroll inside my tableview and so everytime a cell has to be shown on screen, the settings is done again, causing a lag.
I've tried to put settings inside viewDidLoad by using a class variable for my cell but with no effect. Do you have any idea where I could move all of my cells' settings but especially if it's possible ?
Thanks in advance and have a nice day !
EDIT :
As asked, here is my cellForRowAtIndexPathMethod :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.delegate = self;
if ([appliedTheme hasPrefix:#"DarkMode"]) {
self.tableView.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0];
cell.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0];
cell.cardView.backgroundColor = [UIColor colorWithRed:0.30 green:0.30 blue:0.30 alpha:1.0];
cell.webServerIntroLabel.textColor = [UIColor whiteColor];
cell.webServerOptionsTitle.textColor = [UIColor whiteColor];
cell.webServerOptionsReachableAtLabel.textColor = [UIColor whiteColor];
}else {
self.tableView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0];
cell.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0];
cell.cardView.backgroundColor = [UIColor whiteColor];
cell.webServerIntroLabel.textColor = [UIColor blackColor];
cell.webServerOptionsTitle.textColor = [UIColor blackColor];
cell.webServerOptionsReachableAtLabel.textColor = [UIColor blackColor];
}
if ([CellIdentifier isEqualToString:#"webServerIntro"]) {
cell.webServerIntroImageView.image = [UIImage imageNamed:webServerIntroIconToShow];
cell.webServerIntroLabel.adjustsFontSizeToFitWidth = YES;
cell.webServerIntroLabel.numberOfLines = 0;
[cell.webServerIntroLabel sizeToFit];
cell.webServerIntroLabel.text = #"Some text";
}else if ([CellIdentifier isEqualToString:#"webServerOptions"]) {
cell.webServerOptionsTitle.adjustsFontSizeToFitWidth = YES;
cell.webServerOptionsTitle.text = NSLocalizedString(#"Web Server Options", nil);
cell.webServerOptionsStatusLabel.adjustsFontSizeToFitWidth = YES;
if (!webServerStarted) {
cell.webServerOptionsImageView.image = [UIImage imageNamed:#"Not-Checked"];
cell.webServerOptionsStatusLabel.text = NSLocalizedString(#"Not Running", nil);
cell.serverURLLabel.text = NSLocalizedString(#"Not Reachable", nil);
cell.ipAddressURLLabel.text = NSLocalizedString(#"Not Reachable", nil);
[cell.startStopWebServerButton setTitle:NSLocalizedString(#"Start Web Server", nil) forState:UIControlStateNormal];
cell.webServerStartedBool = NO;
}else {
NSString *serverURL = [self deviceName];
serverURL = [serverURL stringByReplacingOccurrencesOfString:#" " withString:#"-"];
cell.webServerOptionsImageView.image = [UIImage imageNamed:#"Checked"];
cell.webServerOptionsStatusLabel.text = NSLocalizedString(#"Running", nil);
cell.serverURLLabel.text = [NSString stringWithFormat:#"http://%#.local", serverURL];
cell.ipAddressURLLabel.text = [NSString stringWithFormat:#"%#", webUploader.serverURL];
[cell.startStopWebServerButton setTitle:NSLocalizedString(#"Stop Web Server", nil) forState:UIControlStateNormal];
cell.webServerStartedBool = YES;
}
cell.webServerOptionsStatusLabel.textColor = [UIColor lightGrayColor];
cell.webServerOptionsReachableAtLabel.adjustsFontSizeToFitWidth = YES;
cell.webServerOptionsReachableAtLabel.text = NSLocalizedString(#"Reachable At :", nil);
cell.ipAddressURLLabel.adjustsFontSizeToFitWidth = YES;
cell.ipAddressURLLabel.textColor = [UIColor lightGrayColor];
cell.ipAddressURLLabel.layer.borderWidth = 1.0f;
cell.ipAddressURLLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cell.ipAddressURLLabel.layer.cornerRadius = 5;
cell.serverURLLabel.adjustsFontSizeToFitWidth = YES;
cell.serverURLLabel.textColor = [UIColor lightGrayColor];
cell.serverURLLabel.layer.borderWidth = 1.0f;
cell.serverURLLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cell.serverURLLabel.layer.cornerRadius = 5;
cell.startStopWebServerButton.layer.borderWidth = 1.0f;
cell.startStopWebServerButton.layer.borderColor = [[UIColor clearColor] CGColor];
cell.startStopWebServerButton.layer.cornerRadius = 5;
cell.startStopWebServerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
if ([appliedTheme isEqualToString:#"Default-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0 green:0.478 blue:0.875 alpha:1]; /*#007ADF : Bleu iOS*/
}else if ([appliedTheme isEqualToString:#"Red-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.90 green:0.07 blue:0.00 alpha:1.0]; /*#E61100 : Rouge*/
}else if ([appliedTheme isEqualToString:#"Orange-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.83 green:0.33 blue:0.00 alpha:1.0]; /*#D35400 : Orange*/
}else if ([appliedTheme isEqualToString:#"Yellow-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.95 green:0.61 blue:0.07 alpha:1.0]; /*#F39C12 : Jaune*/
}else if ([appliedTheme isEqualToString:#"Green-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.68 blue:0.38 alpha:1.0]; /*#27AE60 : Vert*/
}else if ([appliedTheme isEqualToString:#"Purple-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.56 green:0.27 blue:0.68 alpha:1.0]; /*#8E44AD : Violet*/
}else if ([appliedTheme isEqualToString:#"Gray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.74 green:0.76 blue:0.78 alpha:1.0]; /*#BDC3C7 : Gris*/
}else if ([appliedTheme isEqualToString:#"DarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.50 green:0.55 blue:0.55 alpha:1.0]; /*#7F8C8D : Gris foncé*/
}else if ([appliedTheme isEqualToString:#"DesaturatedBlue-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.17 green:0.24 blue:0.31 alpha:1.0]; /*#2C3E50 : Bleu désaturé*/
}else if ([appliedTheme isEqualToString:#"VeryDarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0]; /*#333333 : Gris très foncé*/
}else if ([appliedTheme isEqualToString:#"Black-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha:1.0]; /*#000000 : Noir*/
}else if ([appliedTheme isEqualToString:#"DarkModeDefault-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0 green:0.478 blue:0.875 alpha:1]; /*#007ADF : Bleu iOS*/
}else if ([appliedTheme isEqualToString:#"DarkModeRed-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.90 green:0.07 blue:0.00 alpha:1.0]; /*#E61100 : Rouge*/
}else if ([appliedTheme isEqualToString:#"DarkModeOrange-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.83 green:0.33 blue:0.00 alpha:1.0]; /*#D35400 : Orange*/
}else if ([appliedTheme isEqualToString:#"DarkModeYellow-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.95 green:0.61 blue:0.07 alpha:1.0]; /*#F39C12 : Jaune*/
}else if ([appliedTheme isEqualToString:#"DarkModeGreen-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.68 blue:0.38 alpha:1.0]; /*#27AE60 : Vert*/
}else if ([appliedTheme isEqualToString:#"DarkModePurple-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.56 green:0.27 blue:0.68 alpha:1.0]; /*#8E44AD : Violet*/
}else if ([appliedTheme isEqualToString:#"DarkModeGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.74 green:0.76 blue:0.78 alpha:1.0]; /*#BDC3C7 : Gris*/
}else if ([appliedTheme isEqualToString:#"DarkModeDarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.50 green:0.55 blue:0.55 alpha:1.0]; /*#7F8C8D : Gris foncé*/
}else if ([appliedTheme isEqualToString:#"DarkModeDesaturatedBlue-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.17 green:0.24 blue:0.31 alpha:1.0]; /*#2C3E50 : Bleu désaturé*/
}else if ([appliedTheme isEqualToString:#"DarkModeVeryDarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0]; /*#333333 : Gris très foncé*/
}else if ([appliedTheme isEqualToString:#"DarkModeBlack-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha:1.0]; /*#000000 : Noir*/
}
}else {
}
return cell;
}
While you have a lot of code there, nothing seems like it would be dreadfully slow.
It is a minor point in term of performance, but the code that sets the tableview's background colour should be in viewWillAppear rather than cellForRowAt as there is no need to set the background colour each time a cell is dequeued.
You can also simplify this function considerably by moving all of the theme-related code into the cell subclass. Call a function on the cell passing the theme and have the cell set all of that stuff; The same amount of code will be executed, but cellForRowAt will be smaller and easier to read.
Where I think you can get some performance is by eliminating the repeated string operations related to the theme. Create a class or struct that represents your theme and use an enumeration (This stuff is much easier is Swift, but you can still do it in ObjectiveC).
If you do this, then, for example,
if ([appliedTheme hasPrefix:#"DarkMode"])
becomes
if (appliedTheme.darkMode)
A complex string comparison is replaced with a simple boolean check; this is much faster
Similarly, using an enumeration for the theme you get something like
switch (appliedTheme.theme) {
case DarkModeGreen-Theme:
...
Now an expesnsive string comparison has been replaced with a quick integer comparison.
Finally, creating UIColors is relatively expensive. You can move that into your Theme object too. When you create an instance of a Theme, expose the colours as properties. This way you get rid of the switch statement I suggested and creating the colours each time and simply say something like
cell.startStopWebServerButton.backgroundColor = appliedTheme.buttonBackgroundColor
Have to tried prepareforreuse. Apart from loading images I don't see other code that could cause the delay. Few things that might help
Even though imagenamed should return a cache image if it's already loaded, try loading images async with GCD
Is the web service code causing any delay?
Run instruments (time profiler?) to see what is causing the lag or delay

Switch case issue in objective C

This is the code I followed up on a tutorial
- (UIColor *) randomColor {
int rand = arc4random_uniform(5);
NSLog(#"Num is %d",rand);
switch(rand){
case 1 : return [UIColor greenColor];
case 2 : return [UIColor blueColor];
case 3 : return [UIColor orangeColor];
case 4 : return [UIColor purpleColor];
}
return [UIColor blackColor];
}
This was the exact code in the tutorial and as far as I know, if a switch statement does not have a break statement it should fall through and should execute all the cases, but it does not seem to happen, it just breaks out by itself. Is it because of UIColor or am I missing something obvious?
return keyword ends the execution of a function, so your function ends the first case it enters (because you have return in every case)
A quick fix:
- (UIColor *) randomColor {
int rand = arc4random_uniform(5);
NSLog(#"Num is %d",rand);
UIColor* colorToReturn;
switch(rand){
case 1 : colorToReturn= [UIColor greenColor];
case 2 : colorToReturn=[UIColor blueColor];
case 3 : colorToReturn=[UIColor orangeColor];
case 4 : colorToReturn=[UIColor purpleColor];
}
return colorToReturn;
}
The return statement makes the function stop executing. So, if int rand = 1, it will just return the [UIColor greenColor] and the function will stop. Here's the proper code :
- (UIColor *)randomColor {
int rand = arc4random_uniform(5);
UIColor *color;
switch(rand) {
case 1 : color = [UIColor greenColor];
case 2 : color = [UIColor blueColor];
case 3 : color = [UIColor orangeColor];
case 4 : color = [UIColor purpleColor];
default : color = [UIColor blackColor];
}
return color;
}

BackgroundColor of UItableviewCell Notworking

I am setting the Background color of my uitableviewCell like
cell.contentView.backgroundColor = [UIColor colorWithRed:8.0 green:210.0 blue:11.0 alpha:1.0];
its not working, however, if i do like
cell.contentView.backgroundColor = [UIColor grayColor];
it works then. Any help
UIColor has to be defined between 0 and 1 to get the RGB value to work (UIColor class reference):
cell.contentView.backgroundColor = [UIColor colorWithRed:8.0f/255.0f green:210.0f/255.0f blue:11.0f/255.0f alpha:1.0f];

Change Cell Background Every 5 Cells

Hello I want to change the color of my UITableView Cells ever 5 cells, so I would have green for cell one blue for cell two and so on. Then once I hit 5 cells, I want the colors to start over.
This is what I have so far:
if(indexPath.row % 5 == 0){
cell.backgroundColor = [UIColor blackColor];
} else if (indexPath.row % 4 == 0) {
cell.backgroundColor = [UIColor redColor];
} else if (indexPath.row % 3 == 0) {
cell.backgroundColor = [UIColor greenColor];
} else if (indexPath.row % 2 == 0) {
cell.backgroundColor = [UIColor blueColor];
} else if(indexPath.row % 1 == 0) {
cell.backgroundColor = [UIColor orangeColor];
If someone could point me in the right direction I would really appreciate it. Thank you!
You need to modulo against the same number, not different numbers. This should point you in the right direction:
static NSArray* rowColors;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
rowColors = #[[UIColor redColor], [UIColor blueColor], [UIColor greenColor], [UIColor orangeColor], [UIColor yellowColor]];
});
int rowMod = indexPath.row % rowColors.count;
UIColor* color = rowColors[rowMod];
cell.contentView.backgroundColor = color;
This approach does a few things better than your current approach:
It performs % against the same number, which is the core logic problem in what you posted
It dynamically uses rowColors.count, in case you want to add extra colors (e.g., change every 7 rows instead of every 5)
It uses an array so you don't have if/else-if/else-if/else-if getting out of control
It sets backgroundColor on cell.contentView instead of cell, which is the proper way to set a background color
The array is static and only written to once, to ensure good performance and not result in a ton of memory allocation each time you setup a cell
Hope it helps!
I think this is what you're looking for:
if(indexPath.row % 5 == 0)
{
cell.backgroundColor = [UIColor blackColor];
}
else if (indexPath.row % 5 == 1)
{
cell.backgroundColor = [UIColor redColor];
}
else if (indexPath.row % 5 == 2)
{
cell.backgroundColor = [UIColor greenColor];
}
else if (indexPath.row % 5 == 3)
{
cell.backgroundColor = [UIColor blueColor];
}
else if(indexPath.row % 5 == 4)
{
cell.backgroundColor = [UIColor orangeColor];
}
You want to keep the modulus divisor the same -- its the remainder that is actually going to be changing

Poor scrolling performance in UITableView with cell subviews.

I have a UITableView with many rows. Each row has a UIView in it that I edit in the cellForRowAtIndexPath. My problem is that this lags terribly on an iPod touch. I am not sure why this is happening as the iPod should be able to take more than 11 UIViews on the screen at once? Does anyone know why this is happening?
EDIT - here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *colorView = (UIView *)[cell viewWithTag:1];
UILabel *label = (UILabel *)[cell viewWithTag:2];
colorView.layer.cornerRadius = 10;
colorView.layer.borderColor = [UIColor blackColor].CGColor;
colorView.layer.borderWidth = 2.0f;
colorView.layer.masksToBounds = YES;
switch (indexPath.row) {
case 0:
label.text = #"White";
colorView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
break;
case 1:
label.text = #"Olive";
colorView.backgroundColor = [UIColor colorWithRed:0.814 green:0.551 blue:0.119 alpha:1];
break;
case 2:
label.text = #"Dark Blue";
colorView.backgroundColor = [UIColor colorWithRed:0.036 green:0 blue:1 alpha:1];
break;
case 3:
label.text = #"Dark Green";
colorView.backgroundColor = [UIColor colorWithRed:0 green:0.387 blue:0.006 alpha:1];
break;
case 4:
label.text = #"Orange";
colorView.backgroundColor = [UIColor colorWithRed:1 green:0.500 blue:0 alpha:1];
break;
case 5:
label.text = #"Dark Brown";
colorView.backgroundColor = [UIColor colorWithRed:0.399 green:0.269 blue:0.137 alpha:1];
break;
case 6:
label.text = #"Dark Red";
colorView.backgroundColor = [UIColor colorWithRed:0.530 green:0.017 blue:0 alpha:1];
break;
case 7:
label.text = #"Maroon";
colorView.backgroundColor = [UIColor colorWithRed:0.502 green:0 blue:0.251 alpha:1];
break;
case 8:
label.text = #"Yellow";
colorView.backgroundColor = [UIColor colorWithRed:0.865 green:0.864 blue:0.002 alpha:1];
break;
case 9:
label.text = #"Purple";
colorView.backgroundColor = [UIColor colorWithRed:0.460 green:0 blue:0.865 alpha:1];
break;
default:
label.text = #"";
colorView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
break;
}
return cell;
}
Thanks
There are a few things you can do here.
Turn on quartz debugging (on the device using instruments or, as of quite recently, in the simulator). Colour blended layers - you should see mostly green. If you don't, then make sure your new views are marked as opaque. Scrolling transparent views takes a lot of performance.
If that doesn't help, profile using the time profiler in instruments. Where is the time being spent? Should you, for example, be caching the colours you are using for the background?
Your layer properties should ideally only be set once. awakeFromNib in a custom cell subclass would be a good place to do this.

Resources