2010-10-09 46 views
0

我有一個視圖控制器定義如下:目標C - 調用reloadData後的UITableView我的對象屬性爲null /零

@interface SectionController : UITableViewController { 
    NSMutableArray *sections; 
} 
- (void) LoadSections; 

當LoadSection是調用它對NSURLConnection的調用加載一個URL,後者又調用

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    [connection release]; 
    [responseData release]; 

    NSDictionary *results = [responseString JSONValue]; 
    NSMutableArray *jSections = [results objectForKey:@"Items"]; 
    sections = [NSMutableArray array]; 

    for (NSArray* jSection in jSections) 
    { 
     Section* section = [Section alloc]; 
     section.Id = [jSection objectForKey:@"Id"]; 
     section.Description = [jSection objectForKey:@"Description"]; 
     section.Image = [jSection objectForKey:@"Image"]; 
     section.Parent = [jSection objectForKey:@"Parent"]; 
     section.ProductCount = [jSection objectForKey:@"ProductCount"]; 
     [sections addObject:section]; 
     [section release]; 
    } 

    [jSections release]; 
    [results release]; 

    [delegate sectionsLoaded]; 

    [self.view reloadData]; 
} 

的數據正確地分析,我現在有部分充滿了許多項目。

致電[self.view reloadData]強制回調委託方法的cellForRowAtIndexPath然後應在這一點上部分現在再次NIL呈現數據進入細胞但其。

有人可以指出我的錯誤嗎?我必須承認我是客觀的C新手,這可能是一個指針問題。需要做的是在調用reloadData之後保留段的值。

非常感謝。

回答

1

看到新代碼的問題是顯而易見的:

sections = [NSMutableArray array]; 

應該成爲

[sections release]; 
sections = [[NSMutableArray alloc] init]; 

請注意,數組不會再次成爲「無」,反而解除了分配的,你會得到一個無效的引用,它可能(應該)在解引用時產生崩潰。

我建議你閱讀的引用計數內存管理的一些文章,因爲它可能是,如果你是新來的Objective-C,並往往導致誤不明顯(即:自動釋放不是魔術在所有)

+0

對不起,我已編輯的帖子,包括真正的功能,我發佈節變量,雖然好點。問題仍然存在,在調用reloadData之後,節是零。這就像它重置整個viewcontroller。 – Chris 2010-10-09 17:03:07

+0

我已更新答案以反映新代碼。我希望它可以幫助 – Bitgamma 2010-10-09 17:08:34

+0

感謝您的答覆,但沒有任何區別。 – Chris 2010-10-09 17:25:25

0

避免所有內存泄漏的最佳方法是使用屬性@property (nonatomic, retain) NSMutableArray *sections;,您可以確保所有的人員管理工作都可以通過系統正確管理。不要忘記,當您執行setSections時,屬性會保留值,因此您需要在此處傳遞自動釋放對象。

self.sections = [NSMutableArray array]; 

...

[self.sections addObject:section]; 

同時,爲了避免所有問題,儘量讓這應該只生活在這種方法自動釋放所有對象。就像這樣:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]; 

NSDictionary *results = [responseString JSONValue]; 
NSMutableArray *jSections = [results objectForKey:@"Items"]; 
self.sections = [NSMutableArray array]; 

for (NSArray* jSection in jSections) { 
    Section* section = [[[Section alloc] init] autorelease]; 
    section.Id = [jSection objectForKey:@"Id"]; 
    section.Description = [jSection objectForKey:@"Description"]; 
    section.Image = [jSection objectForKey:@"Image"]; 
    section.Parent = [jSection objectForKey:@"Parent"]; 
    section.ProductCount = [jSection objectForKey:@"ProductCount"]; 
    [self.sections addObject:section]; 
} 

[delegate sectionsLoaded]; 

[self.view reloadData]; 

}

也是最對象的你試圖釋放已經自動釋放: 所有PARAMS傳遞給你的方法不應該被手動釋放,檢查我想JSONValue也應該回報自動釋放對象和任何你通過枚舉或通過調用objectForKey: