2012-12-29 88 views
1

我有一個由存儲被初始化在NSMutableArray的項目如下從Web服務解析XML的程序:NSMutableArray的初始化僅一次

- (id)init { 

    self = [super init]; 
    if (self) 
    { 
     items = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

我用滾動型委託檢查時,用戶點擊的tableview的底部,其中我打電話給Web服務的另一個頁面加載已獲取項目底部的更多項目。

但是,當tableview重新加載時,它不會在已經提取的底部添加新項目,而只會出現新項目。

在我可以設置insertrowsatindexpaths與動畫之前,我相信NSMutableArray再次被初始化,使舊的條目消失,只顯示新的。我如何使NSMutableArray只初始化一次,所以當它獲得新的條目時,它會將它們加載到已獲取的可變數組中。

編輯

根據該意見,看起來像我這樣做精與MutableArray及其得到初始化一次。然後,數據源一定有問題。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[channelFeed items] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ItemsViewCell *cell = (ItemsViewCell *)[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    if (cell == nil) { 
     cell = [[ItemsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
    } 

    FeedItem *item = [[channelFeed items] objectAtIndex:[indexPath row]]; 

    cell.titleLabel.text = [item title]; 
    cell.pubDateLabel.text = [item pubDate]; 
    cell.creatorLabel.text = [item creator]; 
    [[cell thumbImage] setImage:[item thumbnail]]; 

    return cell; 
} 
+0

看起來沒問題。你可以發佈你如何添加項目? – yinkou

+0

我認爲你應該顯示更多的代碼 –

+0

該數組僅在初始化viewcontroller時分配,因此可能只有一次。 plz添加代碼,將項目插入數組 –

回答

3

我會初始化一個更方便的地方陣列。顯然,你的代碼調用init,它將重新初始化數組並刪除它的內容。