【问题标题】:Remove section from tableview if rowsInSection = 0如果 rowsInSection = 0,则从 tableview 中删除部分
【发布时间】:2014-02-24 09:00:04
【问题描述】:

我的 UITableView 中有几个部分,其中有时可以有 0 行。如果 numberOfRowsInSection 等于 0,我如何删除节标题?

这是我的部分代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, tableView.frame.size.width, 22)];
    [label setFont:[UIFont boldSystemFontOfSize:14]];
    [label setTextColor:[UIColor whiteColor]];
    NSString *string =[[theLeagueArray objectAtIndex:section] objectForKey:@"league"];
    [label setText:string];
    [view addSubview:label];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 22)];
    [imgView setImage:[UIImage imageNamed:[[theLeagueArray objectAtIndex:section] objectForKey:@"img"]]];
    [view addSubview:imgView];

    [view setBackgroundColor:[UIColor colorWithRed:13/255.0f green:78/255.0f blue:102/255.0f alpha:1.0f]];

    return view;
}

【问题讨论】:

  • 将您的行数代码放在此处
  • 您可以使用 UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;并在数据发生任何变化后使用 reload tableview

标签: ios objective-c uitableview


【解决方案1】:

查看tableView:heightForHeaderInSection: 的参考文档,并在您需要隐藏节标题时使其返回0(在tableView:viewForHeaderInSection: 中也返回nil 以减少总计算量)。

此外,如果您以 iOS 7 为目标,您可以使用新的 tableView:estimatedHeightForHeaderInSection: 进行更多优化。

【讨论】:

    【解决方案2】:

    试试deleteSections:withRowAnimation: 方法。这将使用可选动画将其从视图中移除,而不会影响您的支持数据。 (但是,无论如何,您都应该更新数据,以免该部分再次出现。)

    请查看UITableView class reference

    【讨论】:

      【解决方案3】:

      基本上,正确的做法是在表格视图上执行deleteSections:withRowAnimation:,并且不要忘记适当地更新数据源。

      但您也可以返回高度为 0.1 的 UIView(这有点骇人听闻),因此在这种情况下用户将看不到它。要更新部分,请执行reloadSections:withRowAnimation: 方法。 它看起来像这样:

      -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
      {
          if ([tableView numberOfRowsInSection:section] == 0) {
              return [[UIView alloc] init];
          }
      ... 
      }
      

      【讨论】:

        【解决方案4】:
        -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
        {
            // If array count 0
                if([[workoutarray objectAtIndex:section] count]==0)
                {
                    return nil;
                }
                else {
        
        
                }
        }
        -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
        {
             // If array count 0
            if([[workoutarray objectAtIndex:section] count]==0)
                {
                    return 0;
                }
                else
                {
                    return 25;
                }        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-02
          • 1970-01-01
          • 2021-11-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多