【问题标题】:Display some grouped tableview sections according the state of the first section, how?根据第一部分的状态显示一些分组的tableview部分,如何?
【发布时间】:2011-02-20 17:25:24
【问题描述】:

我想要一个分组的表格视图,这样:

    1234563
  • 当这个状态改变时,我会在第一个下面看到其他部分;

我怎样才能做到这一点?一些代码/链接来获得类似的东西?

谢谢,

弗兰

【问题讨论】:

    标签: iphone uitableview sections


    【解决方案1】:

    没问题,只需在所有 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 个部分。但是您可以轻松地根据自己的需要采用它。

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 2020-10-20
      • 2014-01-30
      • 2012-09-28
      • 1970-01-01
      • 2016-05-19
      相关资源
      最近更新 更多