我有一个tableViewController,在:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
我有这个代码:
MinorGoal *minor = [self.array objectAtIndex:indexPath.row];
if (minor.finishedBoolean == [NSNumber numberWithBool:YES]){
// code to make check mark
UITableViewCell *indexPathCell = [tableView cellForRowAtIndexPath:indexPath];
indexPathCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"the %@, is finished", minor.title);
}
cell.textLabel.text = minor.title;
我想要的只是检查"次要"的"完成布尔值"arrtibut,如果它是 YES,那么它所在的单元格必须有一个复选标记,当我运行上面的代码时,我得到 NSLog:
NSLog(@"the %@, is finished", minor.title);
这意味着 if 语句正在工作,但为什么单元格附件没有设置为 checkMark?这是哪一行:
indexPathCell.accessoryType = UITableViewCellAccessoryCheckmark;
谢谢,对新手感到抱歉
我想CoreData ManagedObject是minor
对象,它将使finishedBoolean
成为NSNumber
。
您的检查失败,因为您正在检查两个对象是同一对象。== 不检查对象的相等值。
有几种方法可以做到这一点 if 语句。
if ([minor.finishedBoolean boolValue])
if ([minor.finishedBoolean isEqualToNumber:@YES])
if ([minor.finishedBoolean boolValue] == YES)
在所有这些中,我会选择第一个。
希望这有帮助。