Reputation: 13
I have a text field and used numberOfRowsInSection for creating more than 50 text fields. After that I use the if else condition, but it was time consuming and a lengthy method so I want to reduce my if else condition. I don't want to use the switch condition. What should I do?
if (textField.tag == 0)//cust add line 1
{
[_customerFormTableView setContentOffset : CGPointMake(0, 0)];
}
else if (textField.tag == 1)//cust add line 2
{
[_customerFormTableView setContentOffset : CGPointMake(0, 0)];
}
else if (textField.tag == 2)//kyc line 1
{
[_customerFormTableView setContentOffset : CGPointMake(0, 50)];
}
else if (textField.tag == 3)// kyc line 2
{
[self.view endEditing : YES];
DatePickerViewController *dateViewContrl = [self.storyboard instantiateViewControllerWithIdentifier : @"DatePickerViewController"];
dateViewContrl.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
//dateViewContrl.preferredContentSize=CGSizeMake(325, 200);
dateViewContrl.preferredContentSize = CGSizeMake(290, 200);
}
popcontrol = [[WYPopoverController alloc] initWithContentViewController:dateViewContrl];
[popcontrol.delegate self];
// _currentfield=_dateTextField;
NSLog(@"%f %f",popcontrol.popoverContentSize.height,popcontrol.popoverContentSize.height);
CGRect rect_ = [self.view convertRect : textField.frame fromView : textField.superview];
[popcontrol presentPopoverFromRect : rect_
inView : self.view
permittedArrowDirections : WYPopoverArrowDirectionAny
animated : YES
options : WYPopoverAnimationOptionScale];
return NO;
}
else if (textField.tag == 4)
{
[textField resignFirstResponder];
[self DropDownGendview : textField];
return NO;
}
else if (textField.tag == 5)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 250)];
}
else if (textField.tag == 6)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 310)];
}
else if (textField.tag == 7)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 370)];
}
else if (textField.tag == 8 || textField.tag == 13 || textField.tag == 20)
{
[self.view endEditing : YES];
[self DropDownview : textField];
return NO;
}
else if (textField.tag == 9)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 510)];
}
else if (textField.tag == 10)//cor email
{
[_customerFormTableView setContentOffset : CGPointMake(0, 630)];
}
else if (textField.tag == 11)//desig
{
[_customerFormTableView setContentOffset : CGPointMake(0, 700)];
}
else if (textField.tag == 12)//level
{
[_customerFormTableView setContentOffset : CGPointMake(0, 770)];
}
else if (textField.tag == 14)//level
{
[_customerFormTableView setContentOffset : CGPointMake(0, 910)];
}
else if (textField.tag == 15)//level
{
[_customerFormTableView setContentOffset : CGPointMake(0, 980)];
}
else if (textField.tag == 16)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1050)];
}
else if (textField.tag == 17)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1140)];
}
else if (textField.tag == 18)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1240)];
}
else if (textField.tag == 19)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1310)];
}
else if (textField.tag == 21)
{
NSLog(@"state dropdown %ld",(long)textField.tag);
}
else if (textField.tag == 22)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1520)];
}
else if (textField.tag == 23)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1590)];
}
else if (textField.tag == 24)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1660)];
}
else if (textField.tag == 25)
{
[_customerFormTableView setContentOffset : CGPointMake(0, 1730)];
}
else if (textField.tag == 26)
{
[self.view endEditing : YES];
[self dropDownPurposeView : textField];
return NO;
}
Upvotes: 0
Views: 68
Reputation: 184
You can represent a lot of these if statements with data, using a struct:
typedef struct
{
int tag;
int pointX;
int pointY;
} Values;
const Values values[] = {
{0, 0, 0},
{1, 0, 0},
{2, 0, 50},
// etc
};
then iterate through values and determine if your tag is in it:
int i;
bool found = false;
for (i = 0; i < sizeof(values) / sizeof(values[0]); i++)
{
if (values[i].tag == textField.tag)
{
[_customerFormTableView setContentOffset: CGPointMake(values[i].pointX,
values[i].pointY)];
found = true;
break;
}
}
if (! found)
{
// do more complicated operations here
}
you can speed up the for statement with a binary search, if the tags are sorted.
Upvotes: 1
Reputation: 9609
You can try with switch case statement.It is better one instead of having lots of if else condition.If you use swit case it will improve your performance rather than having multiple if else condition.
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
Generally the expression used in a switch statement must have an integral or enumerated type or be of a class type in which the class has a single conversion function to an integral or enumerated type.
For more details go with this https://www.tutorialspoint.com/objective_c/switch_statement_in_objective_c.htm
Upvotes: 0