2009-10-13 40 views
0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
//NSLog(@"Array: %@",rows); 
    return [rows count];// AT THIS LINE 
} 

程序接收到的信號:「EXC_BAD_ACCESS」的UITableView給錯誤

THANKS FOR其實我把它連接到的網頁通過NSURL我在那裏做了一個PHP數組,我創建了一個NSLog的,我的答覆 我得到的數組形式的值,但當它優於行返回[rows count] ;.當我靜態寫入return 2;然後執行時,它會發生錯誤。我正在向你解釋我在做什麼。我與

Name tableViewController=[[JsonTestViewController alloc] initWithNibName:@"JsonTestViewController" bundle:nil]; 

初始化筆尖在JsonTestViewController.m 我有這樣的代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    //NSLog(@"Array: %@",rows); 
    return [rows count]; 
} 
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Configure the cell. 
    NSDictionary *dict = [rows objectAtIndex: indexPath.row]; 
    NSString *strlb1=[dict objectForKey:@"block"]; 
    NSString *strlb2=[dict objectForKey:@"name"]; 
    strlb1=[strlb1 stringByAppendingString:@" , "]; 
    strlb1=[strlb1 stringByAppendingString:strlb2]; 
    NSString *[email protected]"FPS : "; 
    NSString *str2=[dict objectForKey:@"p_hours"]; 
    NSString *strpinf; 
    if([str2 isEqualToString:@"FP"]) 
    { 
     [email protected]"Free Parking"; 
    } 
    else if([str2 isEqualToString:@"12"]) 
    { 
     [email protected]"2 hours"; 
    } 
    else if([str2 isEqualToString:@"14"]) 
    { 
     [email protected]"4 hours"; 
    } 
    else if([str2 isEqualToString:@"MP"]) 
    { 
     [email protected]"Metered Parking"; 
    } 
    str1=[str1 stringByAppendingString:strpinf]; 
    cell.textLabel.text =strlb1; 
    cell.detailTextLabel.text = str1; 



    return cell; 
} 
- (void)viewDidLoad { 

    [super viewDidLoad]; 
    NSURL *url = [NSURL URLWithString:@"SITE URL"]; 
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; 

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding]; 
    NSError *error = nil; 


    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error]; 
    if (dict) 
    { 
     rows = [dict objectForKey:@"users"]; 
    } 

    NSLog(@"Array: %@",rows); 
    [jsonreturn release]; 
} 



- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 
+0

你可以請發表代碼創建行嗎?它看起來像你沒有保留行,並且在調用-tableView:numberOfRowsInSection:時,行已經被自動釋放。 – 2009-10-13 06:16:56

+0

我已發佈代碼 – IPhoneCrazy 2009-10-14 07:44:01

回答

0

你可以給更多的信息?這可以是任何事情,但最有可能的是,行被指向有效數組曾經存在的內存。你是如何創建行數組的?

例如,如果通過另一種方法中的工廠方法將行數組創建爲自動釋放對象,則您的行數組或字典不再指向有效內存。

這裏還有一個問題,這是非常接近你描述:

EXC_BAD_ACCESS signal received

編輯:

所以看你提供的代碼,這些線路也有一些可能性:

NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error]; 
    if (dict) { rows = [dict objectForKey:@"users"]; } 

deserializeAsDictionary方法可以返回自動釋放字典或NULL。所以有一種可能性是rows = NULL。當你嘗試[行數]時,你的程序會崩潰。檢查並看看有什麼錯誤,可能會給你一些線索。

即使您明確返回2作爲numberOfRowsInSection:,也會導致錯誤,因爲在cellForRowAtIndexPath:中,您仍然嘗試訪問行,即使它可能爲NULL。

另一種可能性在於您如何定義行。我猜這是你班上的一個財產。但是,如果您有rows=[dict objectForKey:@"users"];,那麼在方法完成後,行可以不指向任何內容。行將仍然具有[dict objectForKey:]所在的地址,但是在該方法的作用域之後,dict可能會消失,並且所有數據都會隨之而來。

NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error]; 

根據KVC指南,您應該期望字典在方法結束後自動釋放。

另一種可能性是,因爲我不知道您使用的JSON類的細節,因此當您釋放jsonreturn時,您還將釋放與其關聯的所有數據。所以實際上,行沒有指向任何東西。

這種情況下,錯誤似乎根植於您如何設置/保留/訪問行。

嘗試使用Build-> Build &在xcode中分析。它可能會給你一些更多的提示。或者全部投擲一堆NSLog(@"%d",[rows count]);。也嘗試使用調試器。它會給你一些導致[行數]錯誤的方法調用。

+0

感謝您的回覆。但我已經檢查了你已經給出的選項。當我寫回歸時,它崩潰[rows count];而不是返回1,2;當我寫入類型return 2;代碼在[UITableView layoutSubviews]中給出錯誤。但我已經檢查NSLog行作爲數組返回,並且數組數爲10.請讓我知道我在做什麼錯誤錯誤消息我得到的是 IPhoneCrazy 2009-10-14 10:22:44

+0

在此先感謝 – IPhoneCrazy 2009-10-14 10:23:28