我正在努力解決我一直試圖修復3天的問題,而且我需要幫助。iOS5在UITableView中選擇一個NSObject = EXC_BAD_ACCESS
在我的TicketTableViewController.m文件中,我創建了一個NSMutableArray的自定義NSObject(Ticket),每個Ticket都有值;
Ticket *ticketFound = [[Ticket alloc] initWithTicketNumber:ticketString locationNumber:locationString clerkNumber:clerkString approvedState:approvedState approvedDetail:approvedDetail totalNumber:[totalString floatValue] tipNumber:[tipString floatValue] balanceNumber:[subtotalString floatValue] ticketItemsArray:nil ticketItemsDict:nil];
我將這些添加到數組取決於票證的審批狀態;
if ([approvedState isEqualToString:@"approved"]) {
[self.approvedTickets addObject:ticketFound];
}
else if ([approvedState isEqualToString:@"declined"]) {
[self.declinedTickets addObject:ticketFound];
}
else if ([approvedState isEqualToString:@""]){
[self.openTickets addObject:ticketFound];
}
然後我加入這些陣列NSDictionary中的,然後向全球NSArray *tickets
按this tutorial;
NSDictionary *openDict = [NSDictionary dictionaryWithObject:self.openTickets forKey:@"tickets"];
NSDictionary *decDict = [NSDictionary dictionaryWithObject:self.declinedTickets forKey:@"tickets"];
NSDictionary *approvDict = [NSDictionary dictionaryWithObject:self.approvedTickets forKey:@"tickets"];
[self.tickets addObject:openDict];
[self.tickets addObject:decDict];
[self.tickets addObject:approvDict];
這一全球NSArray *tickets
集,然後我就可以顯示在切片的UITableView每票(按照教程)。我使用自定義單元格從數組中的每張票單中提取信息並顯示它們;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TicketCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dictionary = [self.tickets objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"tickets"];
Ticket *ticket = [array objectAtIndex:indexPath.row];
UILabel *locationLabel = (UILabel *)[cell viewWithTag:100];
locationLabel.text = [NSString stringWithFormat:@"Location: %@", ticket.locationNumber];
UILabel *ticketLabel = (UILabel *)[cell viewWithTag:101];
ticketLabel.text = [NSString stringWithFormat:@"Ticket: %@", ticket.ticketNumber];
UILabel *balanceLabel = (UILabel *)[cell viewWithTag:102];
balanceLabel.text = [NSString stringWithFormat:@"$%.2f", ticket.balanceNumber];
return cell; }
這對我來說是完美的工作(雖然這個問題一旦解決就會出現另一個問題)。我的問題出現在用戶從UITableView中選擇一張票時。如果我嘗試調用選定行中票據的任何部分數據,則應用程序崩潰時會出現EXC_BAD_ACCESS
錯誤。這是我的代碼;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictionary = [self.tickets objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"tickets"];
Ticket *ticket = [array objectAtIndex:indexPath.row];
NSLog(@"float: %@", ticket.balanceNumber); //this causes the crash }
我知道我返回票(NSObject的),因爲如果我把NSLog(@"class: %@", [ticket class]);
,改變Ticket *ticket...
到id ticket...
它返回一個票證(NSObject的)。
我明白任何建議。
在此先感謝。
問候,
丹
編輯:這有什麼事與我試圖返回一個float和使用%@
而非%f
- 即使我嘗試NSLog(@"ticketNumber: %@", ticket.ticketNumber);
它是一個字符串,應用程序崩潰。
那麼它可能會崩潰,因爲你沒有使用%。2f在NSLog語句中。 – 2012-07-16 18:50:20
另外,爲什麼你不只是讓門票陣列的數組?我不明白你爲什麼把它們放在字典中,這似乎是一個額外的,不必要的步驟。只需讓'[self.tickets objectAtIndex:[indexPath.section]]返回數組而不需要額外添加任意字典。 – 2012-07-16 18:53:52
但是,我將如何分開不同的數組顯示在部分?我只使用了一個NSDictionary,因爲這就是教程使用的內容,工作流程似乎比我之前的「更緊密」。 – 2012-07-16 19:38:07