Сбой в cellForRowAtIndexPath - PullRequest
       3

Сбой в cellForRowAtIndexPath

1 голос
/ 23 августа 2011

Я получаю сбой в моем приложении со следующей трассировкой стека. Я не вставляю никаких объектов в массив в моем cellForRowAtIndexPath и все еще вижу это в журналах.

- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {  
MyTableViewCell *aCell = nil;
NSString *aCellType = nil;
if (!self.isEditMode){
    if (iIndexPath.row < [self.locationList count]) {
        aCellType = [NSString stringWithFormat:@"%d", DefaultCell];
        aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
        if (!aCell) {
            aCell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
        }
        aCell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSDictionary *aLocationData = [self.locationList objectAtIndex:iIndexPath.row];
        aCell.textLabel.text = [aLocationData stringForKey:@"locations"];
        aCell.textLabel.accessibilityLabel = [NSString stringWithFormat:@"%@ %@", @"My label", 
                                              [aLocationData stringForKey:@"location"]];
    } 
} else {
    if (iIndexPath.row == 0 && self.isEditMode) {
        aCellType = [NSString stringWithFormat:@"%d", AddCell];
        aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
        if (!aCell) {
            aCell = [[[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
        }
        aCell.selectionStyle = UITableViewCellSelectionStyleBlue;
        aCell.isGroupedView = YES;
        aCell.delegate = self;
        [aCell.actionButton setImage:[UIImage imageNamed:@"My.png"] forState:UIControlStateNormal];
        aCell.textLabel.text = @"Data";
        aCell.textLabel.accessibilityHint = [NSString stringWithFormat:@"%@ %@", @"My Data", 
                                             @"Location"];
    } else if (iIndexPath.row < [self.locationList count] + 1) {
        aCellType = [NSString stringWithFormat:@"%d", DefaultCell];
        aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
        if (!aCell) {
            aCell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
        }
        aCell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSDictionary *aLocationData = [self.locationList objectAtIndex:iIndexPath.row - 1];
        aCell.textLabel.text = [aLocationData stringForKey:@"locations"];
        aCell.textLabel.accessibilityLabel = [NSString stringWithFormat:@"%@ %@", @"My Data", 
                                              [aLocationData stringForKey:@"Location"]];
    }
}

return aCell;

}

0   libsystem_kernel.dylib          0x364aca1c __pthread_kill + 8
1   libsystem_c.dylib               0x361db3b4 pthread_kill + 52
2   libsystem_c.dylib               0x361d3bf8 abort + 72
3   libstdc++.6.dylib               0x33b81a64 __gnu_cxx::__verbose_terminate_handler() + 376
4   libobjc.A.dylib                 0x33c2c06c _objc_terminate + 104
5   libstdc++.6.dylib               0x33b7fe36 __cxxabiv1::__terminate(void (*)()) + 46
6   libstdc++.6.dylib               0x33b7fe8a std::terminate() + 10
7   libstdc++.6.dylib               0x33b7ff5a __cxa_throw + 78
8   libobjc.A.dylib                 0x33c2ac84 objc_exception_throw + 64
9   CoreFoundation                  0x349a1ef6 -[__NSArrayM insertObject:atIndex:] + 466
10  CoreFoundation                  0x349a1d14 -[__NSArrayM addObject:] + 28
11  MyApp                           0x0005c520 -[MyController tableView:cellForRowAtIndexPath:] (MyController.m:268)

1 Ответ

0 голосов
/ 23 декабря 2011

Моя предыдущая попытка ответа была неправильной, но при более внимательном рассмотрении проблемы я замечаю, что вы создаете идентификаторы своей ячейки с помощью +NSString stringWithFormat:, которая возвращает автоматически освобожденную строку, которую вы не сохраняете:

    aCellType = [NSString stringWithFormat:@"%d", AddCell];
    aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
    if (!aCell) {
        aCell = [[[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
    }

Если этот NSString будет освобожден до того, как вы его используете, то вы, вероятно, не получите обратно ячейку таблицы, которую ожидаете получить.Что произойдет, если вы retain свой NSString и явно освободить его, когда вы закончите с этим?Вот так ...

    aCellType = [[NSString stringWithFormat:@"%d", AddCell] retain];
    aCell = (MyAddCell *)[iTableView dequeueReusableCellWithIdentifier:aCellType];
    if (!aCell) {
        aCell = [[[MyAddCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellType] autorelease];
    }
    [aCellType release];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...