2012-02-18 18 views
2

これは簡単な質問ですが、正しい解決策を見つけられません。iPhoneでスイッチをオン/オフにしてテーブルビューのセルを非表示にする

私のアプリでは、項目の数を一覧表示するテーブルビューを作成しました。

セルの最初の行にスイッチ(ON/OFF)があります。

私のクライアントは、スイッチがOFFの場合、下のセル(0番目のセル以外)を非表示にし、スイッチがONの場合はすべてのセルを表示する必要があります。

コードでスイッチを作成しました。

誰でもこの方法を教えてもらえますか?

ありがとうございます。

答えて

6

numberOfRows DataSourceメソッド - それはのtableViewと呼ばれるUITableViewDataSourceメソッドがあります1とreloadTableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    /// .... 
      self.switchView = [[[UISwitch alloc] initWithFrame:CGRectZero]autorelease]; 
      cell.accessoryView = self.switchView; 
      [self.switchView setOn:NO animated:NO]; 
      [self.switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 

//.... 

} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if [self.switchView.on] 
     return 1; 
    else 
    { 
     // normal code return 
    } 
} 

- (void) switchChanged:(id)sender { 
    [self.tableView reloadData]; 
} 
+0

@mbh、それはUITableVIewDataSource法ではなく、デリゲート方法です。 –

+0

ありがとうございました。説明するコードも追加されました。 – mbh

+0

iOSのネイティブ設定アプリと同様に、アニメーションが少ない下のセルを非表示にすることはできますか? –

1

を返しますnumberOfRows:切取りは、スイッチの状態をチェックし、それがOFFですか、すべての場合は1を返しmethod.Just行がONの場合ONとOFFを切り替えるたびに[tableView reloadData]メソッドを呼び出すだけです。それと同じくらい簡単です。

+0

reloadDataメソッドが呼び出されると、次のメソッドが順番に呼び出されます。1. tableView:numberOfSections:2 tableView:numberOfRows:inSection:3. tableView:cellForRowAtIndexPath:methodfs。ですから、あなたはONとOFFを切り替えるときにそれを呼び出さなければなりません。 –

5

あなたは、これが追加されますだけでなく、新しい行に

を追加するアニメーションが最初UISwitchにターゲットUIControlEventTouchUpInsideを追加する、insertRowsAtIndexPathsを使用することができます。

[switch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];

- (無効)switchChange:(UISwitch *)送信者{

 isSwitchOn = sender.on; //edited 
    [myTable beginUpdates]; 
    NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0]; 
    NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0]; 
    //you may add as many row as you want here. 

    [myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationMiddle]; 
    [myTable endUpdates]; 

}

とはあなたが挿入されますどのように多くの行を追加しますことを確認してくださいnumberOfRowsInSection:

  • (NSInteger)のtableView:(のUITableView *)のtableView numberOfRowsInSection:(NSInteger)セクション {

    IF(isSwitchOn)リターン1。

    else return 3; //

}

に切り替えた後、必要がありますどのように多くの行あなたは別のセクションを挿入し、上記と同様の操作を行うために[myTable insertSections:withRowAnimation:をチェックすることをお勧めします。オフに切り替えた場合deleteRowsAtIndexPaths:を実装

:しかし、あなたはnumberOfSectionsInTableView:

UPDATEで、追加されますどのように多くのセクション述べることを忘れないでください。

- (ボイド)switchChange:(UISwitch *)差出人{

isSwitchOn = sender.on; 

    if(isSwitchOn){ 
     [myTable beginUpdates]; 
     NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0]; 
     NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0]; 
     //you may add as many row as you want here. 

     [myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationMiddle]; 
     [myTable endUpdates]; 
    } 
    else{ 
     [myTable beginUpdates]; 
     NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0]; 
     NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0]; 
     //you may add as many row as you want here. 

     [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationFade]; 
     [myTable endUpdates]; 
} 

}

関連する問題