我有一段代码,其中我想要一个不同的 accessoryView 用于 TableView 单元格,基于该单元格条目的编号。我设置的代码是:
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");
}
但是,它始终只返回 warriors == 0 的第一个条目。我做错了什么?
最佳答案
与其这样做...
else if (0 < warriors < 50) {
cell.accessoryView = firstLevel;
// NSLog(@"50");
}
这样做...
else if (0 < warriors && warriors < 50) {
cell.accessoryView = firstLevel;
// NSLog(@"50");
}
编辑
要回答您的评论...您可能是想在其中添加一些 <= 或 >=,因为当 warriors 等于您的 if 条件(50、100 或 500)的边界时,它将转到最后一个 else。
你可能希望它看起来像这样......
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");
}
关于ios - 对 NSInteger 使用多个 else if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551765/