0

私のアプリには、tableView の設定方法を決定するスイッチがあります。片側を選択すると、xml フィードのすべてのアイテムが表示されますが、反対側を選択すると、デバイスにダウンロードされたアイテムのみが表示されます。アプリを最初に開くと、デフォルトですべてのアイテムが表示され、すべての行を問題なく選択できます。また、ダウンロードを表示するように選択すると、問題なくすべてを選択できます。ただし、「すべて表示」に戻るときに、さらに下にある表のセルを選択すると、アプリがクラッシュします。ダウンロードされたアイテムの数よりもはるかに下の行を選択した場合にのみクラッシュするため、これは numberOfRowsInSections 呼び出しと関係があると思われますが、私の人生では修正できないようです!

そのメソッド内の私のコードは次のとおりです。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section         {

if (showDownloadsSwitch.selectedSegmentIndex == 0){

    rows = itemsToDisplay.count; 
}
else if (showDownloadsSwitch.selectedSegmentIndex == 1){
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0]
    stringByAppendingPathComponent:@"downloads"];

    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
   error:&error];
    rows = fileList.count;
}
return rows;
}

編集:

もう少し調べたら直りました。[downloads objectAtIndex:indexPath.row] でファイルをチェックしてから、テーブルにすべてのアイテムではなくダウンロードが読み込まれたことを確認していたことがわかりました。助けてくれてありがとう!

4

3 に答える 3

0

行を格納するためにプロパティを使用しないでください。ローカル変数にします。デリゲート メソッドの外でテーブルの行数にアクセスする必要がある場合は、ロジックを個別に再度実行します。

ただし、fileList ローカル変数は、プロパティである必要があるようです。didSelectRowAtIndex メソッドで simar 配列を参照していますね。そこに矛盾が生じる可能性はありますか?

アップデート:

- (void)viewDidLoad
{

  self.itemsToDisplay      = ...
  self.fileList            = ...
  self.showDownloadsSwitch = ...

  [self.showDownloadsSwitch addTarget:self.tableView
                               action:@selector(reloadData)
                     forControlEvents:UIControlEventValueChanged];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 0 && self.showDownloadsSwitch.selectedSegmentIndex == 0)
    return self.itemsToDisplay.count;

  if (section == 1 && self.showDownloadsSwitch.selectedSegmentIndex == 1)
    return self.fileList.count;

  return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0)
    [self.itemsToDisplay objectAtIndex:indexPath.row];
  else
    [self.fileList objectAtIndex:indexPath.row];
}
于 2013-07-10T21:19:37.530 に答える