UITable view: didselectrowatIndexPath for multiple rows
I have a table view(HomeViewController) consisting of items as:
locations >
Reporting >
Setting >
i am able to do this with the help of "didselectrowatIndexPath" for a single row but when i am trying to do so with multiple rows(if else construct), not getting error but still unable to click on any one (locations,reporting or setting).I have imported .h files of all three above.my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (开发者_开发百科[[menuList objectAtIndex:indexPath.row] isEqual:@"LOCATIONS"])
{
LocationViewController *locationViewController;
locationViewController = [[LocationViewController alloc] initWithNibName:@"LocationViewController" bundle:nil];
locationViewController.menuList = [menuList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:locationViewController animated:YES];
}
else if([[menuList objectAtIndex:indexPath.row] isEqual:@"REPORTING"])
{
Reporting *reporting;
reporting = [[Reporting alloc] initWithNibName:@"Reporting" bundle:nil];
reporting.menuList = [menuList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:reporting animated:YES];
}
//[locationViewController release];
}
Also want to discuss about release statement help me! Thanks
isEqual
tests the object's equality to another object. If the strings in your menuList
array are all in upper case then this is fine. If they're like they are in your example before the code then you're going to have problems. Also, if they're both NSStrings then you should use isEqualToString
rather than isEqual
. You can test this by doing something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *arrayValue = [menuList objectAtIndex:indexPath.row];
NSString *myValue = @"LOCATION";
NSLog(@"array value: '%@' my value: '%@'",arrayValue,myValue);
}
The release is not valid because the object is "out of scope".
An objects scope is the current "visible" code base for that variable. Here are some examples:
- (void)aRandomFunction {
/* here is a variable/object. Its scope is the whole function because it has been
declared directly in the function. All constructs have access to it (within the function) */
NSString *myString = @"My String";
if(YES){
NSLog(@"%@", myString); // myString is visible here because its in scope.
}
}
- (void)anotherRandomFunction {
if(YES){
/* here, because we've declared the variable within the if statement
it's no longer a direct object of the function. Instead its a direct
child of the if statement and is therefore only "visible" within that
if statement */
NSString *myString = @"My String";
NSLog(@"%@", myString); // myString is visible here because its in scope.
}
NSLog(@"%@", myString); // but NOT available here because it is out of scope
}
So in essence, a variable's scope is its direct parent construct and all its parent's children constructs.
So there is two ways to do your example. My favourite is this way:
- (void)aFunctionToPushAViewController {
UIViewController *nextPage = NULL;
if(YES){
nextPage = [[CustomViewController alloc] initWithNibName:nil bundle:nil];
}
else {
nextPage = [[ADifferentViewController alloc] initWithNibName:nil bundle:nil];
}
[self.navigationController pushViewController:nextPage animated:YES];
[nextPage release];
}
or... you can just release it in the if statement...
- (void)aFunctionToPushAViewController {
if(YES){
CustomViewController *nextPage = [[CustomViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nextPage animated:YES];
[nextPage release];
}
else {
ADifferentViewController *nextPage = [[ADifferentViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nextPage animated:YES];
[nextPage release];
}
}
精彩评论