【问题标题】:How to hide All section header in UITableViewcell (grouped style)?如何隐藏 UITableViewcell 中的所有部分标题(分组样式)?
【发布时间】:2017-07-16 02:32:52
【问题描述】:

我在UITableviewCell 中有两个部分。我想隐藏那两个部分。 这是我的代码。

 -(CGFloat)tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
    return CGFLOAT_MIN;
}
else
{
    return 32.0f;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [storename2 count];
}

 - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
    return nil;
}
else
{
     return [storename2 objectAtIndex:section];
}
}

在 Viewdidload 中。

- (void)viewDidLoad 
{
[super viewDidLoad];
 self->CartTableview.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}

但它唯一的隐藏第一部分, 显示下图我想要这样(当卡片为空时隐藏部分以及当我将产品添加到卡片时显示全部(两个)部分。) 帮帮我,

显示第一张图片。 当我将产品添加到卡时,将显示两个部分,当我删除产品时,所有部分都将隐藏。 提前致谢。

【问题讨论】:

  • sectionHeader 高度返回 0.1 对我有用。

标签: ios objective-c uitableview height


【解决方案1】:

使用 switch-case 来处理多个截面高度。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    let headerHeight: CGFloat

    switch section {

    case 0:
        // first section for test - set value for height
        headerHeight = 0

    default:
        // other Sections - set value for height
        headerHeight = 0
    }

    return headerHeight
}

【讨论】:

    【解决方案2】:

    只需在 heightForHeaderInSection tableview 委托方法中返回 0...

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        // Specific section
        if (section == 0)
                return 0.0f;
    
        // all sections
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      另外别忘了设置 UITableview Delegate & datasource。

      - (void) viewDidLoad {
          [super viewDidLoad];
           self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
      }
      
      - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
          // Specific section
          if (section == 0)
                  return 0.0f;
      
          // all sections
          return 0;
      }
      
      - (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
      {   // Specific section
          if (section == 0) {
              return nil;
          } else {
          // all sections
              // return some string here ...
          return nil;
          }
      }
      

      【讨论】:

      • 请说明您要在此处隐藏什么(在您的图片中) 1. 带有天蓝色背景和标题交付费用的视图或 2. 带有多条灰色线条的白色视图(令人垂涎的背景在中心)或 3.bottom 蓝色区域(视图)与标题结帐?
      • 是的,但是你在这张图片中的部分是什么。带有天蓝色背景和标题交付费用的视图是部分还是您添加了单独的视图?带有几条灰色线条的白色视图(中心是令人垂涎的背景)是行分隔符(表示行),而不是部分。 PL。说清楚,这张图片中你的部分是什么
      • 麻烦您了,我是ios新手。
      • Np,请告诉我,这张图片中有哪些部分
      • 是的,那是......我是对的,它们是你的 tableview 行分隔符。此时(当您的表格/购物车为空时)隐藏您的表格或从表格视图中删除行分隔符。
      【解决方案4】:

      我认为你需要改变下面的方法

      -(CGFloat)tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section
      {
          return CGFLOAT_MIN;
      }
      

      并删除此方法

      - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
      {
         if (section == 0)
         {
           return nil;
         }
         else
         {
           return [storename2 objectAtIndex:section];
         }
      }
      

      这将隐藏表格视图中的所有部分标题。

      【讨论】:

      • 你是如何确定你的购物车是空的?,或者给我你的数组名称,然后我可以在那里添加条件
      猜你喜欢
      • 2013-10-04
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多