2012-01-04 5 views
1

私のアプリケーションコードの一部としてrssパーサーがあり、うまく動作していて、rss xmlファイルを読み込んでテーブルビューを読み込んでいます。Xcodeテーブルビューのリロードが継続的にテーブルにデータを追加します

問題は、rssデータをリロードするリフレッシュ/リロードボタンで問題になりますが、新しいデータをテーブルにAPPENDSして、テーブルのサイズが大きくなり、サイズが大きくなります。

古いテーブルのデータをクリアして新しいデータでテーブルを再構築することで、テーブルには常に1つのデータセットが表示され、リロード/リフレッシュが実行されるたびに増加することはありません。次のように

テーブルのビルドのコードは次のとおりです。

- (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は、もはや存在しない変数で接続することを意味します。あなたのコード例で

、私は、次の2つのコード行で参照するには問題が見ることができます:あなたがアニメーション化を開始befor activityIdicatorを解放

[activityIndicator release]; 
[activityIndicator startAnimating]; 

を。

機能の最後に解放してください。

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

合意しましたが、これは問題ではありません。上に示したコードスニペットは、悪い順序であっても機能します。 BAD_ACCESSが発生する行[stories removeAllObjects]を追加するときだけです。この1行ではクラッシュし、1行はうまくいきますが、リロードする前にテーブルをクリアしません。 – Richard

関連する問題