10

これは簡単なはずですが、問題があります。

必要がない場合にプログラムで削除したいセルを持つ静的UITableViewがあります。

私はそれのためのIBOutletを持っています

IBOutlet UITableViewCell * cell15;

そして、私はそれを呼び出すことによってそれを削除することができます

cell15.hidden = true;

これはそれを隠しますが、セルがあった場所に空白スペースを残し、私はそれを取り除くことができません。

おそらくハックはそれの高さを0に変更することでしょうか?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath
{
//what would I put here?
}

本当にありがとう!

4

7 に答える 7

13

静的テーブルではデータソース メソッドを実装していないため、データソースでこれを実際に処理することはできません。高さは道のりです。

これを試して:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden
        return 0.0;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 

アップデート

自動レイアウトでは、これが最善の解決策ではない可能性があります。ここに役立つ別の回答があります。

于 2012-04-25T07:01:16.567 に答える
6

tableView:willDisplayCellとをセル識別子と共に使用しtableView:heightForRowAtIndexPathて静的セルを表示/非表示にすることができますが、 ではなく を参照してtableview実装する必要があります。これらの2つの方法は私にとってはうまくいきます:heightForRowAtIndexPathsuperself

(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) {
    [cell setHidden:YES];
    }
}

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) {
        return 0;
}
    return cell.frame.size.height;
}
于 2012-12-26T19:51:26.350 に答える
3

テーブルがどのように機能するかによってtableView:numberOfRowsInSection:、必要なロジックに基づいてセクションの 0 行を返すようにデータ ソースに実装できます。

あなたのコメントのために更新されました:

section パラメーターは、実装が呼び出されたときに iOS によって入力されるため、必要なのは、削除/非表示にした行を含むセクションを処理するためのスイッチだけです。以下の例:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section) {
        case 0:  // first section of your table, change for your situation
             return 0;
        default:
             return 0;
    }
}
于 2012-04-25T06:15:06.827 に答える
0

非表示にするセルをコードのどこかで非表示に設定します。このコードを追加します: (セルの行の高さが異なる場合は、さらに関数をオーバーライドする必要があります)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rowCount=0;
    for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){
        NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
        if (!cell.hidden){
            ++rowCount;
        }
    }
    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int realRow=-1;
    for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){
        NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section];
        UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
        if (!cell.hidden){
            ++realRow;
        }
        if (realRow==indexPath.row)
            return cell;
    }
    return nil;
}
于 2015-09-23T09:13:12.610 に答える