I have an UITableView that contains 3 NSArrays and 3 NSDictionaries for each array.
- (void)viewDidLoad {
[super viewDidLoad];
contentArray = [[NSMutableArray alloc] init];
self.settingsArray = [NSArray arrayWithObjects:@"Settings", nil];
NSDictionary *settingsDict = [NSDictionary dictionaryWithObject:settingsArray forKey:@"Settings"];
self.infoArray = [NSArray arrayWithObjects: @"Version 1.0", @"© Copyrights 2010", @"Developer Site", @"App Page", @"Report a Bug", nil];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:infoArray forKey:@"Settings"];
self.contactArray = [NSArray arrayWithObjects: @"Developer Site", @"App Page", @"Report a Bug", nil];
NSDictionary *contactDict = [NSDictionary dictionaryWithObject:contactArray forKey:@"Settings"];
[contentArray addObject:infoDict];
[contentArray addObject:settingsDict];
[contentArray addObject:contactDict];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[infoArray objectAtIndex:indexPath.row] isEqual:@"Version 1.1"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if ([[infoArray objectAtIndex:indexPath.row] isEqual:@"© Copyrights 2010"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if ([[settingsArray objectAtIndex:indexPath.row] isEqual:@"Settings"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"NULL" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show]开发者_运维百科;
}
if ([[contactArray objectAtIndex:indexPath.row] isEqual:@"Developer Site"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if ([[contactArray objectAtIndex:indexPath.row] isEqual:@"App Page"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if ([[contactArray objectAtIndex:indexPath.row] isEqual:@"Report a Bug"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
The problem is when I'm trying to select a row, the application is crashing
Thanks
That error will occur when you attempt to access an index beyond the bounds of an array. For example, you'll encounter an NSRangeException if your array has only one item and you ask for the object at index 3. The immediate solution is to check the array's size before querying its contents.
NSArray* exampleArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
int items = [exampleArray count];
if(indexPath.row < items) {
// do stuff
}
精彩评论