2013-09-17 2 views
0

У меня есть очень простое приложение, чтобы узнать, как работать с разделами в UITableView, но есть исключение -Исключение при пытаться прокручивать UITableView

2013-09-17 08: 46: 19.956 Разделы [4497: c07] * - [__ NSArrayI objectAtIndex]: сообщение, отправленное высвобождены например 0x9566d40

целые методы ниже - нужна помощь.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; 
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    self.names = dict; 
    NSArray *array = [[_names allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
    _keys = array; 
} 

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSLog(@"%lu", (unsigned long)[_keys count]); 
    return [_keys count]; 
} 

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSString *key = [_keys objectAtIndex:section]; 
    NSArray *nameSection = [_names objectForKey:key]; 
    return [nameSection count]; 
} 

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    NSString *key = [_keys objectAtIndex:section]; 
    NSArray *nameSection = [_names objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier]; 
    } 

    cell.textLabel.text = [nameSection objectAtIndex:row]; 
    return cell; 
} 

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *key = [_keys objectAtIndex:section]; 
    return key; 
} 
+0

вы пробовали отладить его? на какой линии он падает? –

+0

в методе - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath в этой строке NSString * key = [_keys objectAtIndex: section]; – ShurupuS

ответ

1

Вы должны сохранить _keys массив так:

_keys = [[[_names allKeys] sortedArrayUsingSelector:@selector(compare:)] retain]; 
+0

Это работает - но это пример из книги. Но есть версия ARC, и я пытаюсь управлять памятью вручную. Итак, как я вижу, проблема здесь .. thx! – ShurupuS

1

Здесь вы первым принимает значения в другой массив, а затем передать его в _ keys ..thats не правильный способ сделать это ..

только непосредственно передавать значения _keys как ниже

_keys = [[_names allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

также проверьте на self.names, вы делаете то же самое.

Надеюсь, это поможет вам.

Смежные вопросы