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]
てデータをロードします。1回目は正常に動作しますが、2回目は で失敗しEXC_BAD_ACCESS code=2
ます。なんで?
編集:
エラーはへの呼び出しから来ているようです
#0 0x01b8c2a3 in TComponentFont::GetMinSideBearing(CGAffineTransform const&, bool) const ()
またはへの呼び出し
これが役立つことを願っています。
編集:
また、NSZombies からの助けもありません。Xcodeで(NSZombiesを使用して)実行すると、同じエラーが出力されず、ゾンビプロファイルでプロファイリングするとメッセージが表示されず、アプリがクラッシュします。
編集:
このエラーは、セクションのタイトルから発生しています。これらのセクションをコメントアウトすると、エラーが発生しなくなります。セクション タイトルの実装について何が間違っていますか?
編集:
これは_headers
、DirectoryViewController.h で宣言されている方法です。
@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;
}