其他期待的表达

时间:2014-08-11 03:00:12

标签: objective-c if-statement

为什么'否则'在这种情况下要求表达式?

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{


    if(indexPath.row % 2 == 0)
        cell.contentView.backgroundColor = [self colorWithHexString:@"42403E"];
        cell.accessoryView.backgroundColor = [self colorWithHexString:@"42403E"];
    else
        cell.contentView.backgroundColor = [self colorWithHexString:@"494744"];
        cell.accessoryView.backgroundColor = [self colorWithHexString:@"494744"];

}

但是当我使用时它不会出现:

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{


    if(indexPath.row % 2 == 0)
        cell.contentView.backgroundColor = [self colorWithHexString:@"42403E"];
    else
        cell.contentView.backgroundColor = [self colorWithHexString:@"494744"];

}

谢谢!

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{


    if(indexPath.row % 2 == 0){
        cell.contentView.backgroundColor = [self colorWithHexString:@"42403E"];
        cell.accessoryView.backgroundColor = [self colorWithHexString:@"42403E"];
    }
    else{
        cell.contentView.backgroundColor = [self colorWithHexString:@"494744"];
        cell.accessoryView.backgroundColor = [self colorWithHexString:@"494744"];
    }

}

如果在条件语句之后有多行代码,最好有代码块。然后编译器更容易解析流程。

希望这有帮助