18

IB/ストーリーボードの静的セルを使用して作成されたテーブル ビュー フォームがあります。ただし、特定の条件に応じて、実行時に一部のセルを非表示にする必要があります。

私はいくつかの「答え」を見つけました。SOに関するこの質問に、例えば

静的セルに設定された UITableView。プログラムで一部のセルを非表示にすることは可能ですか?

..そして、彼らはセル/行の高さを0に設定することに焦点を当てています。これは素晴らしいことですが、制約を満たすことができないためにAutoLayoutから例外が発生することを除いて. この最後の問題を回避するにはどうすればよいですか? サブビューの自動レイアウトを一時的に無効にすることはできますか? iOS7でこれを行うより良い方法はありますか?

4

9 に答える 9

14

これを行う最善の方法は、単純に numberOfRowsInSection、cellForRowAtIndexPath、および heightForRowAtIndexPath を処理して、特定の行を選択的に削除することです。これは私のシナリオの「ハードコードされた」例です。このようにハードコードするのではなく、特定のセルをインテリジェントに削除するために少し賢くすることができますが、私の単純なシナリオではこれが最も簡単でした。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0 && hideStuff) {
        cell = self.cellIWantToShow;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
    if (indexPath.section == 0 && hideStuff) {
        height = [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    }
    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = [super tableView:tableView numberOfRowsInSection:section];

    if (section == 0 && hideStuff) {
        count -= hiddenCells.count;
    }

    return count;
}
于 2013-09-23T03:14:58.623 に答える
3

セルの下端に触れる制約がないことを確認した場合、自動レイアウトはバーフしないはずです (iOS 6.0、6.1、7.0 でテスト済み)。あなたはまだ上端に「固定」し、高さを固定する必要があります. (もちろん、逆にして下に固定することもできます。)

レイアウトが上端と下端の両方の位置に依存している場合は、プログラムで制約を削除できる場合があります (結局のところ、それらは単なるオブジェクトです)。

于 2013-12-28T20:57:58.927 に答える
2

これを行うコーシャの方法は、動的セルを使用することです。行の高さを 0 に設定するのはハックです。静的セルは非常に便利ですが、機能が制限されています。

于 2013-09-22T05:20:18.097 に答える
1

アニメーションを行さえできる方法を見つけ、iOS 8.3 で作業しています。tableView:numberOfRowsInSection:データ ソース メソッドを実装し、UITableView メソッド insertRowsAtIndexPaths:withRowAnimation:およびdeleteRowsAtIndexPaths:withRowAnimation:で行を追加/削除するだけです。

UISwitchの状態に基づいて正確な行を非表示にする例を次に示します。

- (IBAction)alowNotif:(id)sender {
    UISwitch *sw = (UISwitch*)sender;
    NSIndexPath *index = [NSIndexPath indexPathForRow:5 inSection:0];
    if ([sw isOn]) {
        [self.tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else {
        [self.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (![notifications isOn]) {
        return 5;
    }
    return 6;
}

上で@algalが述べたように、 numberOfRowInSection :はまだUITableViewDataSourceメソッドであるため、どれだけ長く機能するかを単純に知ることはできません。

于 2015-04-28T23:31:53.337 に答える
0

私にとって最善の方法は、numberOfRowsInSection メソッドを変更することでした。表示したくないデータソースを削除しました。すべてが1つの機能にあるため、私にとって最良のソリューションです。

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section==0) {
    NSUInteger productCount = _products.count;
    for(loop trough your data) {
      if(your condition is true)
         productCount--;
      }
    }
    return productCount;
} else
    return self.groups.count;
}
于 2014-04-08T09:24:50.080 に答える