开发者

Declare CGRect in if statement

I have an if statement that checks if an array has a certain item:

    if ([[subTitles objectAtIndex:indexPath.row] isEqualToString:@""]) {

and has this code inside it:

CGRect CellFrame = CGRectMake(0, 0, 300, 44);

Then I have an else statement with the same thing but with different 开发者_运维知识库values, obviously.

It looks like this all together:

if ([[subTitles objectAtIndex:indexPath.row] isEqualToString:@""]) {
    CGRect CellFrame = CGRectMake(0, 0, 300, 44);
} else {
    CGRect CellFrame = CGRectMake(0, 0, 300, 60);
}

Later in my code, I am trying to use CellFrame but it says it is undeclared. What is the proper way to make a CGRect if/else statement without having to put all of the code in each statement? Please say if I am unclear so I can provide more detail. Thanks.


You need the variable to be defined outside of the if/else statement. When you put it inside, it is only visible to code in that statement and will be destroyed on exit. Also, since only the height is different, you could assign the other parts of the rectangle outside of the if statement.

CGRect CellFrame;
CellFrame.origin = CGPointMake(0,0);
CellFrame.size.width = 300;
if([[subTitles objectAtIndex:indexPath.row] isEqualToString:@""]) {
    CellFrame.size.height = 44;
} else {
    CellFrame.size.height = 60;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜