2012-07-16 123 views
0

我正在努力解決我一直試圖修復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 *ticketsthis 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);它是一個字符串,應用程序崩潰。

+0

那麼它可能會崩潰,因爲你沒有使用%。2f在NSLog語句中。 – 2012-07-16 18:50:20

+0

另外,爲什麼你不只是讓門票陣列的數組?我不明白你爲什麼把它們放在字典中,這似乎是一個額外的,不必要的步驟。只需讓'[self.tickets objectAtIndex:[indexPath.section]]返回數組而不需要額外添加任意字典。 – 2012-07-16 18:53:52

+0

但是,我將如何分開不同的數組顯示在部分?我只使用了一個NSDictionary,因爲這就是教程使用的內容,工作流程似乎比我之前的「更緊密」。 – 2012-07-16 19:38:07

回答

0

我明白了!我在我的自定義NSObject類中使用assign而不是retain,因爲它的屬性。感謝您的幫助!

+0

歡迎使用stackoverflow。請記住稍後爲未來的訪問者接受您的答案。 – Shahbaz 2012-07-17 13:21:15

0

什麼類型是balanceNumber?你是用%@打印/記錄一個數字,如果它是一個雙精度型,嘗試%lf

0

我的猜測是Ticket類「balanceNumber」中的屬性是float或decimal,因此你的nsLOG需要是這樣的:當你的進程空間外訪問內存

if float: 

NSLog(@"float: %f", ticket.balanceNumber); 

if decimal: 


NSLog(@"float: %d", ticket.balanceNumber); 
+0

感謝您的快速回復奧斯卡。對不起,我應該已經說得更清楚了。即使我嘗試調用ticket.ticketNumber這是一個字符串,應用程序崩潰。 – 2012-07-16 19:35:50

0

一般來說,EXC_BAD_ACCESS發生,要麼是因爲內存已被釋放,或者因爲指針已被損壞指向某個進程空間之外。你沒有提到你是否在這個項目中使用ARC,這會對可能的罪魁禍首產生一些影響。無論哪種方式,我會嘗試運行NSZombieEnabled,看看是否有幫助。

相關問題