8

基本的に、cellForRowAtIndexPath関数は UITableViewCell を返す必要があります。私のコードでは、特定の値が見つかった場合に何かをチェックしてセルをスキップする動作をチェックしたいと思います。

これが私が今持っているものです:

static NSString *FirstCellIdentifier = @"First Custom Cell";
static NSString *SecondCellIdentifier = @"Second Custom Cell";

CustomObject *o = [_customObjects objectAtIndex:indexPath.row];

if ([s.name isEqualToString:@""])
{
    FirstCellController *cell = [customList dequeueReusableCellWithIdentifier:FirstCellIdentifier];
    if (!cell) { 
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (FirstCellController *) currentObject;
                break;
            }
        }
    }
    // Here I do something with the cell's content
    return cell;
}
else {
    SecondCellController *cell = [customList dequeueReusableCellWithIdentifier:SecondCellIdentifier];
    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SecondCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (SecondCellController *) currentObject;
                break;
            }
        }
    }
    // Here i do something with the cell's content
    return cell;
}

私がやりたいのは、s.nameが空でない場合、セルを表示せずに「スキップ」して次のセルに移動したいということです。

誰でもこれについてアドバイスがありますか?

ありがとう。

4

3 に答える 3

22

この方法でセルを「スキップ」することはできません。データソースがn行があると主張している場合は、それぞれにセルを提供する必要があります。正しい方法は、データソースを変更して(n-1)行を削除するときに行を要求し、UITableViewreloadDataを呼び出してテーブルを再生成することです (そして、表示されている行ごとに新しいセルを要求します)。

別のオプションは、行/セルを「非表示」にすることです。これに使用した手法は、指定されたセルの高さを 0 にすることですheightForRowAtIndexPath

于 2012-10-11T16:02:19.470 に答える
1

トムが言ったように、UITableViewDelegate メソッドで「スキップ」しようとしないでください。代わりに、このロジックを UITableViewDataSource メソッドに入れます。例として、データを除外する一般的なメソッドを設定できます。

- (NSArray*)tableData {
    NSMutableArray *displayableObjects = [NSMutableArray array];
    [displayableObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        CustomObject *customObject = (YourCustomObject *)obj;
        if (customObject.name && ![customObject.name isEqualToString:@""]) {
            // only show in the table if name is populated
            [displayableObjects addObject:customObject];
        }
    }];
    return displayableObjects;
}

次に、データソース メソッドで、それを使用して、テーブルに表示する事前にフィルター処理されたデータを取得します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self tableData] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomObject *o = [[self tableData] objectAtIndex:indexPath.row];
    ....
}

このように、 reloadData が呼び出されるたびに、セルを構築するときにフィルター処理されたデータが常にスキップされます。

于 2012-10-11T16:21:10.730 に答える