UITableView reloadData EXC_BAD_ACESS代码= 2

时间:2013-09-27 03:40:50

标签: ios objective-c uitableview automatic-ref-counting exc-bad-access

我有用于加载UITableView的代码:

- (int)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.peopleTableView)
        return [self.people count];
    else
        return [[[self.scheduleDays objectAtIndex:self.dayInt] periods] count];
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.peopleTableView)
        return [[self.people objectAtIndex:section] count];
    else
        return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (tableView == self.peopleTableView)
        return [self.headers objectAtIndex:section];
    else
        return [NSString stringWithFormat:@"%@ - %@", [[[[self.scheduleDays objectAtIndex:self.dayInt] periods] objectAtIndex:section] startTime], [[[[self.scheduleDays objectAtIndex:self.dayInt] periods] objectAtIndex:section] endTime]];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.headers;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return index;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.peopleTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

        Person *person = [[self.people objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
        [[cell textLabel] setText:[NSString stringWithFormat:@"%@ %@", [person firstName], [person lastName]]];

        return cell;
    } else {
        ScheduleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

        if (cell == nil) {
            cell = [[ScheduleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }

        Period *period = [[[[[[self appDelegate] user] scheduleDays] objectAtIndex:self.dayInt] periods] objectAtIndex:[indexPath section]];

        [[cell mainLabel] setText:[period desc]];
        [[cell subtitleLabel1] setText:[period teacher]];
        [[cell subtitleLabel2] setText:[period roomLocation]];

        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.peopleTableView) {
        self.currentViewedPerson = [[self.people objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
        [self loadPerson:self.currentViewedPerson];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.scheduleTable)
        return 70;
    else
        return 44;
}

我使用方法调用[_tableView reloadData]来加载数据。它第一次正常工作,但第二次失败时EXC_BAD_ACCESS code=2。为什么呢?

修改

似乎错误来自对

的调用
#0  0x01b8c2a3 in TComponentFont::GetMinSideBearing(CGAffineTransform const&, bool) const ()

TComponentFont

或致电

enter image description here

我希望这会有所帮助。

修改

NSZombies也没有帮助。在Xcode中运行它(使用NSZombies)我得到相同的错误没有输出,使用僵尸配置文件对其进行分析它没有消息,应用程序只是崩溃。

修改

此错误来自章节标题,因为当我注释掉那些部分时,我不再收到错误。我的章节标题实施有什么不正确之处?

修改

这是在DirectoryViewController.h中声明_headers的方式:

@property (strong, nonatomic) NSMutableArray *headers;

如何填充标题(可能不是全部必需但是......):

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if (parser == self.peopleParser) {
        if ([elementName isEqualToString:@"People"]) {
            self.people = [NSMutableArray array];
            self.headers = [NSMutableArray array];
        } else if ([elementName isEqualToString:@"Person"]) {
            self.currentPerson = [Person new];
            [self.currentPerson setPersonID:[attributeDict objectForKey:@"id"]];
        }
    } else if (parser == self.scheduleParser) {
        if ([elementName isEqualToString:@"Schedule"])
            self.scheduleDays = [NSMutableArray array];
        else if ([elementName isEqualToString:@"Day"]) {
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"ScheduleDay" inManagedObjectContext:[[self appDelegate] managedObjectContext]];
            self.currentDay = [[ScheduleDay alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
        } else if ([elementName isEqualToString:@"Course"]) {
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Period" inManagedObjectContext:[[self appDelegate] managedObjectContext]];
            self.currentPeriod = [[Period alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
        }
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    self.currentString = string;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if (parser == self.peopleParser) {
        if ([elementName isEqualToString:@"People"])
            self.currentLetter = @"";
        else if ([elementName isEqualToString:@"Person"]) {
            if ([self.currentLetter isEqualToString:[[[self.currentPerson lastName] substringToIndex:1] uppercaseString]])
                [[self.people lastObject] addObject:self.currentPerson];
            else {
                [self.people addObject:[NSMutableArray array]];
                [self.headers addObject:[[[self.currentPerson lastName] substringToIndex:1] uppercaseString]];
                self.currentLetter = [[[self.currentPerson lastName] substringToIndex:1] uppercaseString];
                [[self.people lastObject] addObject:self.currentPerson];
            }
        } else if ([elementName isEqualToString:@"Last"])
            [self.currentPerson setLastName:self.currentString];
        else if ([elementName isEqualToString:@"First"])
            [self.currentPerson setFirstName:self.currentString];
        else if ([elementName isEqualToString:@"EmailAddress"])
            [self.currentPerson setEmail:self.currentString];
        else if ([elementName isEqualToString:@"PhoneCell"])
            [self.currentPerson setCellPhone:self.currentString];
        else if ([elementName isEqualToString:@"PhoneHome"])
            [self.currentPerson setHomePhone:self.currentString];
        else if ([elementName isEqualToString:@"GradYear"])
            [self.currentPerson setGradYear:self.currentString];
        else if ([elementName isEqualToString:@"StudentGrade"])
            [self.currentPerson setGrade:self.currentString];
        else if ([elementName isEqualToString:@"Street1"])
            [self.currentPerson setStreet1:self.currentString];
        else if ([elementName isEqualToString:@"Street2"])
            [self.currentPerson setStreet2:self.currentString];
        else if ([elementName isEqualToString:@"City"])
            [self.currentPerson setCity:self.currentString];
        else if ([elementName isEqualToString:@"State"])
            [self.currentPerson setState:self.currentString];
        else if ([elementName isEqualToString:@"Zip"])
            [self.currentPerson setZip:self.currentString];
    } else if (parser == self.scheduleParser) {
        if ([elementName isEqualToString:@"Course"])
            [self.currentPeriod setDay:self.currentDay];
        else if ([elementName isEqualToString:@"Day"])
            [self.scheduleDays addObject:self.currentDay];
        else if ([elementName isEqualToString:@"StartTime"])
            [self.currentPeriod setStartTime:self.currentString];
        else if ([elementName isEqualToString:@"EndTime"])
            [self.currentPeriod setEndTime:self.currentString];
        else if ([elementName isEqualToString:@"Description"])
            [self.currentPeriod setDesc:self.currentString];
        else if ([elementName isEqualToString:@"Location"])
            [self.currentPeriod setRoomLocation:self.currentString];
        else if ([elementName isEqualToString:@"Teacher"])
            [self.currentPeriod setTeacher:self.currentString];
    }
    self.currentString = @"";
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    if ([parseError code] == 5) {
        self.people = [NSMutableArray array];
        self.headers = [NSMutableArray array];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:[parseError description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
}

修改

调用reloadData的地方:

- (void)search {
    NSString *urlString = [LINK SETUP CODE GOES HERE]

    self.peopleParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
    self.peopleParser.delegate = self;
    if ([self.peopleParser parse] && [self.people count] > 0) {
        [self.peopleTableView reloadData];
        [self.peopleTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
        [self.view addSubview:self.peopleView];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results!" message:@"Your search returned no results. Try broadening your search." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
    [self.activityIndicator stopAnimating];
    self.activityIndicator.hidden = YES;
}

4 个答案:

答案 0 :(得分:4)

为避免内存问题,您应该使用属性访问器方法创建对象并引用对象。

例如,您为headers可变数组声明了属性:

@property (strong, nonatomic) NSMutableArray *headers;

然后你应该这样创建你的数组:

 self.headers = [NSMutableArray array];

以这种方式引用你的数组:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.headers objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.headers;
}

使用此语法可以调用headers属性的setter和getter方法(这些方法由Xcode自动生成)。

在非ARC环境中,调用(retain, nonatomic)属性的这些方法(retain类似于strong)是等效的:

- (NSMutableArray *)headers {

  return _headers;
 }

- (void)setHeaders:(NSMutableArray *)headers {

    [_headers autorelease];
    _headers = [headers retain];
}

如果在ARC环境中使用self.表示法,则会自动跟踪对象的引用计数。

您可以找到有关属性here的更多信息。

修改

这似乎是框架中的一个错误。请检查此answer

答案 1 :(得分:0)

第二个是因为自动释放,你应该使用NSLog()检查值,然后你可以非常方便地找到解决方案。在不同的阶段或不同的方法中获取每个值

答案 2 :(得分:0)

在没有看到更多代码的情况下,我的猜测是虽然字符串对于标题数组是等效的,但是你没有在sectionForIndexTitle中正确地比较它们。

请记住,indexOfObject只有在指向字符串的指针完全相同时才有效。拥有两个具有相同值的字符串不一定足够。

为安全起见,您可以尝试:

for (int i = 0; i < [_headers count]; ++i)
{
    if ([_headers[i] isEqualToString title])
    {
        return i;
    }
}
// error handling belongs here

但同样,这是一个猜测,因为我们没有清楚地了解如何构建_headers或者代码中还有其他内容。

答案 3 :(得分:-1)

我认为这次崩溃是由于自动释放TableView对象,'_people'数组或'_headers'数组。您要做的第一件事是记录其值以检查它是否是已发布的对象。 如果是这样,你可以使用

  [_people retain]; 

  [_headers retain];

 [tableviewObject retain]; 

避免自动释放。