1

次のようなグループ化されたテーブルビューを作成します。

  • 最初のセクションの唯一の行が状態に関連付けられている場合、たとえばAの場合、この最初のセクションのみが表示され、その下にテキストが含まれている可能性があります(たとえば、フッター)。

  • この状態が変わると、最初のセクションの下に他のセクションが表示されます。

どうすればこれを達成できますか?同様のものを取得するためのコード/リンクはありますか?

ありがとう、

フラン

4

1 に答える 1

1

問題ありません。すべてのtableViewデータソースとデリゲートメソッドにifelseロジックを追加するだけです。

たとえば、次のようになります。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!canUseInAppPurchase || isLoading) {
        return 1;
    }
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!canUseInAppPurchase || isLoading) {
        return 1;
    }
    if (section == 0) {
        // this will be the restore purchases cell
        return 1;
    }
    return [self.products count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell = ...
    NSString *cellText = nil;
    if (!canUseInAppPurchase) {
        cellText = @"Please activate inapp purchase";
    }
    else if (isLoading) {
        cellText = @"Loading...";
    }
    else {
        if (section == 0) {
            cellText = @"Restore purchases";
        }
        else {
            cellText = productName
        }
    }
    cell.textLabel.text = cellText;
    return cell;
}

2番目のセクションを追加または削除する場合は、単純に[tableViewreloadData]を使用できます。またはこのよりスムーズなバリアント:

[self.tableView beginUpdates];
if (myStateBool) {
    // activated .. show section 1 and 2
    [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withRowAnimation:UITableViewRowAnimationTop];
}
else {
    // deactivated .. hide section 1 and 2
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withRowAnimation:UITableViewRowAnimationBottom];
}
[self.tableView endUpdates];

注意してください。最初にデータソースのデータを変更する必要があります。そして、このコードは2つのセクションを追加します。しかし、あなたはそれをあなたのニーズに簡単に取り入れることができます。

于 2011-02-20T17:41:40.690 に答える