Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphonequestions

macrumors newbie
Original poster
Mar 21, 2010
14
0
Can someone tell me what is wrong with me code here, im getting an error "error: expected expression before 'else'" and im not sure where to go to fix this, the second one seems to be right but when i added a third line i got the error! Any help even documentation would help, thanks!


I think im missing {} but not sure here to put it," } else { " seems to give me the error also



// Configure the cell.
if (indexPath.row % 2)
cell.textLabel.text = [NSString stringWithFormat:mad:"FirstVC", indexPath.row];

else
cell.textLabel.text = [NSString stringWithFormat:mad:"SecondVC", indexPath.row];

else
cell.textLabel.text = [NSString stringWithFormat:mad:"SecondVC", indexPath.row];


return cell;
}
 
If need to do:

if
else if
else if
else if
....
else if
else

You can't have two "else" statements in a row.

You can either do:

if

-----------

if
else

-----------

if
else if
..... (repeat as needed)
else
 
If need to do:

if
else if
else if
else if
....
else if
else

You can't have two "else" statements in a row.

You can either do:

if

-----------

if
else

-----------

if
else if
..... (repeat as needed)
else


Much thanks, ill give that a shot!

ok i put that in and then i get the error:

error: expected '(' before 'cell'

So i am missing }{ then?


Code:
    // Configure the cell.
	if (indexPath.row % 2) 
		cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 

	else if   
		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
	
	else 
		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
	

	return cell;
}
 
They do, but he's also missing a "{ ". If there's code above, he might want to include it.

My apologies, this is right out of the App...
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    
    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    // Configure the cell.
	if (indexPath.row % 2) 
		cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 

	else if   
		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
	
	else 
		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
	
	
					
	return cell;
}
 
else if what? It needs a condition. As in: else if (condition).

Also, the condition should return a boolean. indexPath.row % 2 doesn't.
 
else if what? It needs a condition. As in: else if (condition).

Also, the condition should return a boolean. indexPath.row % 2 doesn't.

So what should that look like? I don't know, thats what im asking.. How do i fix this code so it compiles in my test project?
 
Looks like you can get rid of:

Code:
else if   
cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];

Edit: Just noticed you changed the SecondOneVC to SecondVC. Did you mean to do that?

else if what? It needs a condition. As in: else if (condition).

Also, the condition should return a boolean. indexPath.row % 2 doesn't.

If the modulus is not zero, it's true. If zero, it's false. So, why wouldn't it work the way it is?
 
Looks like you can get rid of:

Code:
else if   
cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];

no no, thats a view, ok i wrote it a bit silly, but still, there are 3 views i want to present in the TableView, FirstView - SecondOneView - SecondView.

It was just the way i wrote it, sorry for any confusion..
 
Looks like you can get rid of:

Code:
else if   
cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];

Edit: Just noticed you changed the SecondOneVC to SecondVC. Did you mean to do that?



If the modulus is not zero, it's true. If zero, it's false. So, why wouldn't it work the way it is?

The only reason the code wouldn't work as is is because there are only two options for the whatever % 2... It's either 1 or 0. So yes it'll work as a condition in an if statement, but you can only have two different conditions. There will be if(! thing%2) .. else if (thing%2)
and if you have an "else" it will never be run.
 
The only reason the code wouldn't work as is is because there are only two options for the whatever % 2... It's either 1 or 0. So yes it'll work as a condition in an if statement, but you can only have two different conditions. There will be if(! thing%2) .. else if (thing%2)
and if you have an "else" it will never be run.

Your saying it will only run with 2 views? Surely that cant be right?! There are lots of Apps using loads more.... Or if i misunderstood you, should there be a "(! thing%2)" after the 'if else' so it will work? Your saying it will work without a condition if there are only two?

I need three, maybe more, so how will i make this work?
 
The only reason the code wouldn't work as is is because there are only two options for the whatever % 2... It's either 1 or 0. So yes it'll work as a condition in an if statement, but you can only have two different conditions. There will be if(! thing%2) .. else if (thing%2)
and if you have an "else" it will never be run.

Yeah, I see that now.
 
The only reason the code wouldn't work as is is because there are only two options for the whatever % 2... It's either 1 or 0. So yes it'll work as a condition in an if statement, but you can only have two different conditions. There will be if(! thing%2) .. else if (thing%2)
and if you have an "else" it will never be run.

That assumes the else if condition is dependent on the same thing as the if condition.

You could say:

if (x % 2)
else if (someRandomFunction())
else
 
Your saying it will only run with 2 views? Surely that cant be right?! There are lots of Apps using loads more.... Or if i misunderstood you, should there be a "(! thing%2)" after the 'if else' so it will work? Your saying it will work without a condition if there are only two?

No I'm saying your logic is wrong. You're trying to set up an if/else statement with more than two conditions, but the number of conditions you will get from something mod 2 is only 2.

so you could do
Code:
if(thing%2) {
//do 1
}
else {
//do 0
 }
or something more like
Code:
if(thing%3 == 1) {
// do 1
}
else if(thing%3 == 2) {
//do 2
}
else {
//do 0
}

but logically the way you were doing it to begin with makes no sense.
 
So,

Code:
    // Configure the cell.
	if (indexPath.row % 3) 
	 cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 

	 else if (indexPath.row % 2)   
		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
	
	 else  
		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
	
	
					
	return cell;
}


Cool, that seems to be working... kinda
 
Your saying it will only run with 2 views? Surely that cant be right?! There are lots of Apps using loads more.... Or if i misunderstood you, should there be a "(! thing%2)" after the 'if else' so it will work? Your saying it will work without a condition if there are only two?

I need three, maybe more, so how will i make this work?

Step one: Use a calculation other than (something % 2). As posted above, any number % 2 will always return one of the value 0 or 1. There are no other outcomes possible. So if you want 3 possible outcomes, 0, 1, or 2, then your calculation must be (something % 3).

Step two: If you want 3 (or more) distinct branches, then a chain of if/else if/.../else is usually a poor way to do it. In C and Objective-C, the switch statement performs a multi-way branch based on a single input value.

http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_php.2C_ActionScript

If the Wikipedia example is too unclear, then you should refer to a C or Objective-C language reference manual or tutorial (a book or an online webpage), and learn how to use switch from that.

The way you're doing it now, it seems that your desire to use 3 or more views has exceeded your skills in the language. No problem, really, it happens all the time when learning. Just realize that you're going to have to learn something new if you want to accomplish this task.
 
So,

...

Cool, that seems to be working... kinda

You need to rethink your logic regarding when to display which view. I don't think you fully understand how the % operator works.

Based on this thread, you may want to take some other users' advice and spend a few days (re)learning programming concepts... these are pretty basic mistakes that are completely due to not understanding the fundamental concepts.

edit: To elaborate a little more... you want to change your conditions to something like:

Code:
    // Configure the cell.
	if (indexPath.row % 3 == 2) 
	 cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 

	 else if (indexPath.row % 3 == 1)   
		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
	
	 else  
		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
					
	return cell;
}

which uses FirstVC if the row is 2, 5, 8, 11, etc... SecondOneVC if the row is 1, 4, 7, 10, etc... SecondVC if the row is 3, 6, 9, 12, etc. Somehow I don't think this is what you're trying to accomplish.
 
edit: To elaborate a little more... you want to change your conditions to something like:

Code:
    // Configure the cell.
	if (indexPath.row % 3 == 2) 
	 cell.textLabel.text = [NSString stringWithFormat:@"FirstVC", indexPath.row]; 

	 else if (indexPath.row % 3 == 1)   
		cell.textLabel.text = [NSString stringWithFormat:@"SecondOneVC", indexPath.row];
	
	 else  
		cell.textLabel.text = [NSString stringWithFormat:@"SecondVC", indexPath.row];
					
	return cell;
}

which uses FirstVC if the row is 2, 5, 8, 11, etc... SecondOneVC if the row is 1, 4, 7, 10, etc... SecondVC if the row is 3, 6, 9, 12, etc. Somehow I don't think this is what you're trying to accomplish.

No not quite, i mean what do i do if i want to show 20 views?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.