2017-07-04 5 views
0

私は自分のアプリにフィルタ関数を追加していますが、フィルタを適用するたびに、フィルタを変更すると、これを実装する方法はありますか?ios - コード(Objective-C)でuitableview内のオープンセルを閉じる方法

私は現在、使用しています:

[self.tableview reloadData]; 

をテーブルにフィルターを開閉する

を適用するたびにリロードする:テーブルビューreloadDataexpandedSections配列をクリアする前に

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      // only first row toggles exapand/collapse 
      [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

      NSInteger section = indexPath.section; 
      BOOL currentlyExpanded = [expandedSections containsIndex:section]; 
      NSInteger rows; 

      NSMutableArray *tmpArray = [NSMutableArray array]; 


      if (currentlyExpanded) 
      { 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
       [expandedSections removeIndex:section]; 

      } 
      else 
      { 

       [expandedSections addIndex:section]; 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
       NSLog(@"Section:%d", rows); 
      } 

      for (int i=1; i<rows; i++) 
      { 
       NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                   inSection:section]; 
       NSLog(@"INDEX: %@", tmpIndexPath); 
       [tmpArray addObject:tmpIndexPath]; 
      } 

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

      if (currentlyExpanded) 
      { 
       dispatch_async(dispatch_get_main_queue(), ^{ 

       [tableView deleteRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationTop]; 
       }); 
      } 
      else 
      { 
        dispatch_async(dispatch_get_main_queue(), ^{ 
       [tableView insertRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationTop]; 
        }); 

      } 
     } 
} 
+0

オープン/クローズコードを追加 – Subramanian

+0

質問を更新しました。 – apex

答えて

1

を。

[expandedSections removeAllIndexes]; 
[self.tableView reloadData]; 

expandedSectionsアレイは、そのビューを折りたたむないことへの拡大部分details.Dueの状態を維持します。そのため、テーブルビューをリロードする前に配列をクリアしてください。

+0

エラーを取得します。 'NSMutableIndexSet'の表示されない@interfaceがセレクタ 'removeAllObjects'を宣言します – apex

+1

NSMutableIndexSetであるため、 'removeAllIndexes'を使用する必要があります。また、 – Subramanian

+0

も更新されました。ありがとう! – apex

関連する問題