Question

I have a section of code, in which I want a different accessoryView for the TableView cell, based off the number for that cell's entry. The code I have set up is:

NSInteger warriors = [entry.prayerWarriors intValue];

        if (warriors == 0) {
            //Do nothing
            //NSLog(@"0");
        }
        else if (0 < warriors < 50) {
            cell.accessoryView = firstLevel;
           // NSLog(@"50");

        }
        else if (51 < warriors < 100) {
            cell.accessoryView = secondLevel;
          //  NSLog(@"100");

        }
        else if (101 < warriors < 500) {
            cell.accessoryView = thirdLevel;
         //   NSLog(@"500");

        }
        else {
            cell.accessoryView = fourthLevel;
         //   NSLog(@"A Lot");

        }

However, it always returns only the first entry for warriors == 0. What am I doing wrong?

Was it helpful?

Solution

Rather than doing this...

else if (0 < warriors < 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");
}

do this...

else if (0 < warriors && warriors < 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");
}

EDIT

To answer your comment...you probably mean to have some <= or >= in there, as it's going to the last else when warriors equals the border of your if conditionals (50, 100 or 500).

You probably want it to look like this...

 NSInteger warriors = [entry.prayerWarriors intValue];

    if (warriors == 0) {
        //Do nothing
        //NSLog(@"0");
    }
    else if (0 < warriors && warriors <= 50) {
        cell.accessoryView = firstLevel;
        // NSLog(@"50");

    }
    else if (50 < warriors && warriors <= 100) {
        cell.accessoryView = secondLevel;
        //  NSLog(@"100");

    }
    else if (100 < warriors && warriors <= 500) {
        cell.accessoryView = thirdLevel;
        //   NSLog(@"500");

    }
    else {
        cell.accessoryView = fourthLevel;
        //   NSLog(@"A Lot");

    }

OTHER TIPS

The statement

if (0 < warriors < 50)

evaluates different than you might think. The first part

(0 < warriors)

evaluates as a boolean, and that boolean will be compared to 50.

So, you need to do: if (0 < warriors && warriors < 50)

The other answers make good points about the later cases needing a logical and but if you are getting stuck at if (warriors == 0) { most likely your object entry.prayerWarriors is nil. Put a break point and print it out. (and print out it's class to make sure its as expected)

Also minor but a good habit to be in your conversion of what I'm guessing is an NSNumber isn't using the same type as your variable. Since you are writing into a NSInteger you should replace intValue with integerValue

For clarity, I prefer to put the variable first in each condition and to encapsulate each part of the condition:

if (warriors == 0) {
 //Do nothing
NSLog(@"0");
}
else if ((warriors > 0) && (warriors < 50))
{
cell.accessoryView = firstLevel;
NSLog(@"50");
}
else if ((warriors > 51) && (warriors < 100)) {
 cell.accessoryView = secondLevel;
 NSLog(@"100");
    }
    else if ((warriors > 101) && (warriors < 500)) {
        cell.accessoryView = thirdLevel;
        NSLog(@"500");
    }
    else {
        cell.accessoryView = fourthLevel;
        NSLog(@"A Lot");
    }

Just be sure you've have enough parens around the conditions.

You do not need an if cascade for this:

You can store the levels one time anywhere

NSArray *levels = @[firstLevel, secondLevel, thirdLevel, fourthLevel];

And use it indexed:

if( warriors > 0) {
  cell.accessoryView = levels[(warriors-1) / 50]
}

But if you want to have an if cascade, you do not have to double check:

NSInteger warriors = [entry.prayerWarriors intValue];

if (warriors == 0) {
    //Do nothing
    //NSLog(@"0");
}
else if (warriors <= 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");

}
else if (warriors <= 100) {
    cell.accessoryView = secondLevel;
    //  NSLog(@"100");

}
else if (warriors <= 500) {
    cell.accessoryView = thirdLevel;
    //   NSLog(@"500");

}
else {
    cell.accessoryView = fourthLevel;
    //   NSLog(@"A Lot");

}

If you are in an else, it is already tested that the preceding condition failed.(That's the meaning of else.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top