проблемы UITableView в нескольких разделах - PullRequest
0 голосов
/ 25 июля 2010

У меня есть UITableView, который содержит 3 NSArrays и 3 NSDictionaries для каждого массива.


    - (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];      
    }
}


Проблема в том, что когда я пытаюсь выбрать строку, происходит сбой приложения

Спасибо

1 Ответ

0 голосов
/ 26 июля 2010

Эта ошибка возникает при попытке доступа к индексу за пределами массива. Например, вы столкнетесь с NSRangeException, если в вашем массиве есть только один элемент, и вы запросите объект по индексу 3. Непосредственное решение - проверить размер массива, прежде чем запрашивать его содержимое.

NSArray* exampleArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
int items = [exampleArray count];
if(indexPath.row < items) {
    // do stuff
}
...