2013-05-17 27 views
1

我很難理解這個,希望有人能幫我解決這個問題。NumberOfRowsInSection第二次調用兩次 - 屬性「未設置」

我有一個屬性叫回覆在我的ViewController。在用戶單擊表中的一行後,由另一個ViewController加載,並將XML節點傳遞到新ViewController上的屬性。

我有這樣的代碼:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (replies == nil) 
    { 
     replies = [[NSArray alloc] init]; 
    } 
    if(replies.count == 0) 
    { 
     replies = [node elementsForName:@"question"]; 
    } 

    return replies.count; 
} 

回帖是在視圖控制器的屬性。 NumberOfRowsInSection在加載過程中被調用兩次。在第一次通過時,我可以看到該屬性被填充。第二個是空的,並且對respond.count的調用返回「發送到釋放實例的消息」錯誤。

我對此很困惑 - 由於該屬性是在第一次通過期間設置的,第二次通過發生時我的價值發生了什麼變化?這已被我逼瘋......

+3

顯示'replies'的聲明。你需要聲明一個'strong'的引用。另外,對於任何與內存相關的問題,您應該提及您是否使用ARC。 – Mar0ux

+1

如果您的「答覆」是屬性,則將其用作屬性self.replies。 – danypata

+1

請注意,如果'reply == nil'兩個'if'分支都將被執行。 –

回答

0

確保您的屬性保留強引用回覆:

@property (nonatomic, strong) NSArray *replies; 

,並參考類似的答覆如下:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.replies == nil) 
    { 
     self.replies = [[NSArray alloc] init]; 
    } 
    if(replies.count == 0) 
    { 
     self.replies = [node elementsForName:@"question"]; 
    } 
    return self.replies.count; 
} 
相關問題