2012-01-04 57 views
1

我有一個RSS解析器爲我的應用程序代碼的一部分,它是工作的罰款,並加載RSS XML文件和填充的tableview罰款。的Xcode表視圖重裝不斷增加的數據表中

問題是與刷新/ Reload按鈕,這不加載RSS數據,但新數據追加到表和表只是增長和增長的規模。

行爲應該做的是清除舊錶數據並用新數據重建表 - 以便表總是隻顯示一組數據,並且在每次按下重新加載/刷新時都不會保持增長。

表生成代碼如下:

- (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]; 
} 

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"]; 
cell.detailTextLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; 

[cell.textLabel setLineBreakMode:UILineBreakModeWordWrap]; 
[cell.textLabel setNumberOfLines:0]; 
[cell.textLabel sizeToFit]; 

[cell.detailTextLabel setLineBreakMode:UILineBreakModeWordWrap]; 
[cell.detailTextLabel setNumberOfLines:0]; 
[cell.detailTextLabel sizeToFit]; 

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
return cell; 
} 

而重載/刷新按鈕的代碼:

- (void)reloadRss { 
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator]; 
[[self navigationItem] setLeftBarButtonItem:barButton]; 
[barButton release]; 
[activityIndicator release]; 
[activityIndicator startAnimating]; 
[self performSelector:@selector(parseXMLFileAtURL) withObject:nil afterDelay:0]; 
[newsTable reloadData]; 
} 

我試圖解決這個通過將行:

if (stories) { [stories removeAllObjects]; } 

給重載部分,我認爲應該工作,並清檯,但隨後的應用程序崩潰,應用智慧h EXC_BAD_ACCESS。

任何意見或建議非常感謝!

回答

1

其實現在已經解決了!

是由於陣列的「自動釋放」的元素,所以清除了這一點之後,他們是無效的。

刪除autorelease,只是在最後的dealloc釋放這些對象的工作。

0

代碼EXC_BAD_ACCESS表示您希望連接到一個不再存在的變量。

在你的代碼示例中,我可以看到,這個問題看,在下面的代碼兩行:

[activityIndicator release]; 
[activityIndicator startAnimating]; 

你釋放activityIdicator befor開始動畫效果。

嘗試在函數的最後釋放。

[activityIndicator startAnimating]; 
[self performSelector:@selector(parseXMLFileAtURL) withObject:nil afterDelay:0]; 
[newsTable reloadData]; 
[activityIndicator release]; 
+0

同意,但這不是問題。上面顯示的代碼段確實有效,即使排序錯誤也是如此。只有當我添加BAD_ACCESS發生的[故事removeAllObjects]行時。有了這條線,它會崩潰,沒有一條線可以正常工作,但在重新加載之前不會清除表。 – Richard 2012-01-04 21:35:20

相關問題