根据第一部分的状态显示一些分组的tableview部分,如何?

我会分组表视图,这样: 当第一部分的唯一一行与一个状态相关联时,比如A,我只能看到第一部分,可能还有一些文本(例如,在一个页脚中); 当这个状态发生变化时,我会看到第一个下面的其他部分; 我怎么能实现这个目标?一些代码/链接获得类似的东西? 谢谢, 弗兰     
已邀请:
没问题,只需在所有tableView数据源和委托方法中添加一些if else逻辑。 例如这样:
- (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;
}
如果你想添加或删除第二部分,你可以使用简单的[tableView reloadData];或者这个更平滑的变体:
[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个部分。但您可以轻松地将其应用于您的需求。     

要回复问题请先登录注册