A small issue over here i have 4 sections when i click on the first TableCell of a section (got 4 sections) the NSlogs output is:
2012-02-20 20:33:16.870 [8880:c07] 00
Can i do something like;
if switch(indexPath.row == 0) {
case
} else if(indexPath.row == 1) {
case
}
to 'link' each Tablecell to another function?
The problem is when i use the code ;
case 0:
NSLog(#"00");
break;
each first Tablecell of a section gives me the NSlog 00
UIViewController *controller;
switch(indexPath.row) {
case 0:
NSLog(#"00");
break;
case 1:
NSLog(#"01");
break;
case 2:
NSLog(#"02");
break;
case 3:
NSLog(#"03");
break;
case 4:
NSLog(#"04");
break;
}
I think you are looking for something like this:
if (indexPath.section == 0) {
switch(indexPath.row) {
case 0:
NSLog(#"00");
break;
case 1:
NSLog(#"01");
break;
case 2:
NSLog(#"02");
break;
case 3:
NSLog(#"03");
break;
case 4:
NSLog(#"04");
break;
}
} else if (indexPath.section == 1) {
switch(indexPath.row) {
case 0:
NSLog(#"10");
break;
case 1:
NSLog(#"11");
break;
case 2:
NSLog(#"12");
break;
case 3:
NSLog(#"13");
break;
case 4:
NSLog(#"14");
break;
}
}
The if statement separates the execution path on the section, and then switch further separates it on the row. If the number of sections is significant, you could use an outer switch outside of the inner switches.
Related
So I have created a switch statement inside another switch statement for certain sections and rows, quite hard to explain so have a look at the actual code. Anyways I was wondering if such patter should be avoided or if it does not really matter, I am new to stackoverflow as you may have already noticed so I am not entirely sure this question has been asked before.
switch(indexPath.section) {
case 0: {
switch ([indexPath row]) {
case 0: {
[self.navigationController pushViewController:introductionViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
case 1: {
[self.navigationController pushViewController:matterViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
break;
}
case 1: {
break;
}
}
}
A switch statement inside of a switch statement is perfectly legal. There's nothing inherently wrong with it, though it may be difficult to follow if there are many cases in either statement. One solution is to make the inner switch statement into a separate function or method. For example, if you have this:
switch (a)
{
case foo:
switch (b)
{
case bar:
doSomething();
break;
case bas:
doSomethingElse();
break;
}
break;
case foo2:
...whatever
}
You could do it like this:
void handleFoo (int x)
{
switch (x)
{
case bar:
doSomething();
break;
case bas:
doSomethingElse();
break;
}
}
...
main ()
{
...
switch (a)
{
case foo:
handleFoo(b);
break;
case foo2:
...whatever;
break;
}
}
I have a switch statement like this:
switch (int) {
case 0:
// do x
break;
case 1:
// do y
break;
default:
break;
}
Can I get the case number?
The reason I ask is that I have a method in each one that would use a variable int. For example:
switch (int) {
case 0:
[self doMethod:string setTag: <CASE NUMBER> ];
break;
case 1:
[self doMethod:string setTag: <CASE NUMBER> ];
break;
default:
break;
}
Is there any way to do this?
Thanks!
switch (x) {
case 0:
// if you're here, you know x == 0
[self doMethod:string setTag: x];
break;
...
}
But unless string is a different variable or literal in each case, this doesn't make much sense; you could as easily say
if (x == 0 || x == 1) {
[self doMethod: string setTag: x];
}
First off, you can't do switch (int). int is a data type. You need a variable in there.
Then all you need to do is reference the variable in the case statement:
int someVar = ... // some value
switch (someVar) {
case 0:
[self doMethod:string setTag:someVar];
break;
case 1:
[self doMethod:string setTag:someVar];
break;
}
BTW - if you do the same thing for multiple cases you can do:
int someVar = ... // some value
switch (someVar) {
case 0:
case 1:
[self doMethod:string setTag:someVar];
break;
default:
// other stuff
break;
}
And as Josh pointed out, depending on your needs, this whole thing could simply be:
int someVar = ... // some value
[self doMethod:string setTag:someVar];
or maybe:
int someVar = ... // some value
if (someVar >= 0 && someVar <= 1) {
[self doMethod:string setTag:someVar];
}
I was wondering if there is a way to combined switch cases for example:
switch (value)
{
case 0,1,2:
nslog (#"0,1,2 cases");
break
case 3:
nslog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
I'll really appreciate your help
You mean, something like this?
switch (value)
{
case 0:
case 1:
case 2:
NSLog (#"0,1,2 cases");
break;
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
You know, the switch case structure will execute each line inside the braces starting from the corresponding case line, until it reach the last one or a break. So, if you don't include a break after a case, it will go on executing the next case also.
Alternatively, you can do this...
case 0:
case 1:
case 2:
NSLog();
break;
case 3:
NSLog()
break;
default:
NSLog();
break;
It is also possible to use ranges (a little bit less code). The following example illustrates it:
switch (value)
{
case 0 ... 2:
NSLog (#"0,1,2 cases");
break
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
Two constants (1+2) share the same case statement. I donĀ“t want to double the code.
What is the right syntax to do this?
switch (expression) {
case 0:
[self taskA];
break;
case 1:
[self taskB];
break;
case 2:
[self taskB]
break;
default:
break;
}
Use :
switch (expression) {
case 0:
[self taskA];
break;
case 1:
case 2:
[self taskB];
break;
default:
break;
}
Edit 1:
In switch we say a term called fall-through. Whenever control reaches to a label say case 0: it falls till break is found. On break control is sent to the closing braces of switch.
If break is not encountered it goes to next case as in case then case 2. So above case 1 and case 2 shares one break statement.
Multiple case labels can refer to the same statement if break or return are not used at the end of the case.
If you do not use a break statement in case 1, the execution flows into case 2.
i have a view contains many sections(group of buttons) each section in the view contains unique id, i want to add buttons to an NSMutableArray according to their id,(i.e. when the user clicks a button i get the button id then add to the array all the buttons have that id) i wrote these code, but when i check the length of the array after adding i found it to be 0 !!
Hint: sec_0,sec_1,sec_2,sec_3,sec_4,sec_5,sec_6,sec_7,sec_8 is a predifined NSMUtable arrays.
-(NSMutableArray*)findButtonsSection:(UIButton *)buton{
NSLog(#"Inside findButtonsSection");
int tag_No=buton.tag;
for (UIButton* bton in self.view.subviews) {
NSLog(#"Inside findButtonsSection2");
switch (tag_No) {
case 0:
[sec_0 addObject:bton];
NSLog(#"Number of buttons in array=%i",[sec_0 count]);
break;
case 1:
NSLog(#"Button tag No=%i",buton.tag);
[sec_1 addObject:bton];
return sec_1;
break;
case 2:
NSLog(#"Button tag No=%i",buton.tag);
[sec_2 addObject:bton];
return sec_2;
break;
case 3:
NSLog(#"Button tag No=%i",buton.tag);
[sec_3 addObject:bton];
return sec_3;
break;
case 4:
NSLog(#"Button tag No=%i",buton.tag);
[sec_4 addObject:bton];
return sec_4;
break;
case 5:
NSLog(#"Button tag No=%i",buton.tag);
[sec_5 addObject:bton];
return sec_5;
break;
case 6:
NSLog(#"Button tag No=%i",buton.tag);
[sec_6 addObject:buton];
return sec_6;
break;
case 7:
NSLog(#"Button tag No=%i",buton.tag);
[sec_7 addObject:bton];
return sec_7;
break;
case 8:
NSLog(#"Button tag No=%i",buton.tag);
[sec_8 addObject:bton];
return sec_8;
break;
default:
NSLog(#"nnnnnnnnnn");
}
}
}
Form a C array out of those sec_x mutable arrays after they're constructed. Like this:
NSMutableArray *secs[] = {sec_0, sec_1....};
Then use indexing instead of switch:
[secs[tag_No] addObject:bton];
The square brackets in this case denote array element access, not an Objective C method call.
On a more general note, read up on plain old C. The Objective C/iOS tutorials often assume, without spelling it out, that you have some C background and start by explaining the Objective C object system rather than the C bits.
The reason your arrays are all empty is because you return after you add any value to an array that isnt sec_0. You don't want those returns, as it will break out of your for loop. Try the code below:
You could also switch the array around to save you some lines of code and improve readibility.
-(NSMutableArray *)findButtonSelections:(UIButton *)button {
NSArray *array = [NSArray arrayWithObjects:sec_0, sec_1, sec_2, sec_3, sec_4, sec_5, sec_6, sec_7, sec_8, nil];
int tag = button.tag;
//BTW, this function will return all views, not just buttons
for (UIButton* bton in self.view.subviews) {
//So add this to make sure the view is a button
if([bton isKindOfClass:[UIButton class]]) {
[[array objectAtIndex:tag] addObject:bton];
}
}
return [array objectAtIndex:tag];
}
If you wanted to keep your switch code, try this below, but i don't recommend it.
-(NSMutableArray *)findButtonSelections:(UIButton *)button {
NSLog(#"Inside findButtonsSection");
int tag_No=buton.tag;
for (UIButton* bton in self.view.subviews) {
NSLog(#"Inside findButtonsSection2");
switch (tag_No) {
case 0:
[sec_0 addObject:bton];
NSLog(#"Number of buttons in array=%i",[sec_0 count]);
break;
case 1:
NSLog(#"Button tag No=%i",buton.tag);
[sec_1 addObject:bton];
break;
case 2:
NSLog(#"Button tag No=%i",buton.tag);
[sec_2 addObject:bton];
break;
case 3:
NSLog(#"Button tag No=%i",buton.tag);
[sec_3 addObject:bton];
break;
case 4:
NSLog(#"Button tag No=%i",buton.tag);
[sec_4 addObject:bton];
break;
case 5:
NSLog(#"Button tag No=%i",buton.tag);
[sec_5 addObject:bton];
break;
case 6:
NSLog(#"Button tag No=%i",buton.tag);
[sec_6 addObject:buton];
break;
case 7:
NSLog(#"Button tag No=%i",buton.tag);
[sec_7 addObject:bton];
break;
case 8:
NSLog(#"Button tag No=%i",buton.tag);
[sec_8 addObject:bton];
break;
default:
NSLog(#"nnnnnnnnnn");
}
}
return [array objectAtIndex:tag];
}