2010-07-25 1 views
0

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


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


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

Благодарности

+0

Какое сообщение об ошибке регистрируется? – Kurbz

+0

2010-07-25 21: 58: 19.355 Двойной поиск [48109: 207] *** Завершение приложения из-за неперехваченного исключения «NSRangeException», причина: *** - [NSCFArray objectAtIndex:]: индекс (4) за пределами границ (1)» 2010-07-25 21: 58: 19,355 Dual Search [48109: 207] Стек: ( 43292752, 44450604 , 43030283, 43030122 , 748457, 187239, 49603, 3416117, 3375658, 330631, 42571740, 42567848 , 51927197, 51927394 , 3056498, 9476,) завершение вызова после вызова экземпляра 'NSException' – 2010-07-25 18:58:47

ответ

0

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

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

Спасибо, но у меня есть несколько массивов. Массив для каждого раздела. – 2010-07-26 06:09:16

+0

Да, но вы пытаетесь получить доступ ко всем трем в didSelectRow. – Justin

+0

Я попробовал и все еще рушился. Я сделал небольшую проверку и все объекты независимо от того, в каком массиве идет последний индекс третьего массива – 2010-07-26 13:36:45