【问题标题】:UITableView - gap between main header and section headerUITableView - 主标题和节标题之间的间隙
【发布时间】:2018-10-14 23:32:02
【问题描述】:

处理UITableView,在主表视图和每个部分都有一个标题。然而,我们在这两个标题之间发现了一个奇怪的差距。我们已经尝试过其他 SO 答案中给出的解决方案,例如:

self.edgesForExtendedLayout

tableView.contentInset

self.automaticallyAdjustsScrollViewInsets = false

但没有运气!

我整理了一张正在发生的事情的屏幕截图:

问题是红色和绿色标题之间的差距,非常感谢任何帮助,如果需要,很乐意分享进一步的代码 sn-ps。突出显示的区域在视图调试器中匹配,因此我确认这不是任何地方的额外填充问题。

一些相关代码:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeader = ShareScreenSectionHeaderView()
    let title = viewData.sections[section].sectionTitle
    let subtitle = viewData.sections[section].sectionSubtitle
    sectionHeader.update(title: title, subtitle: subtitle)
    return sectionHeader
}

表格视图的标题集是UIView,使用tableHeaderView 属性设置

【问题讨论】:

  • 使用heightForHeaderInSection方法
  • 从情节提要中删除页眉和页脚值
  • 代码 sn-p 可以提供帮助
  • @Vinaykrishnan 您希望查看表格视图设置的哪一部分?

标签: ios swift uitableview


【解决方案1】:

这里是代码sn-p

@property (weak, nonatomic) IBOutlet UITableView *tblLoad;
@property (strong, nonatomic) IBOutlet UIView *lbltblHeader;

- (void)viewDidLoad {
[super viewDidLoad];
   self.tblLoad.tableHeaderView = self.lbltblHeader;
   self.tblLoad.delegate = self;
   self.tblLoad.dataSource= self;
  [self.tblLoad reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 5;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
    v.backgroundColor = UIColor.greenColor;
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30) ];
    lbl.text = [NSString stringWithFormat:@"I am the %ld Section ",section];
    lbl.textColor = UIColor.blackColor;
    [v addSubview:lbl];
    return v;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"CellReuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
        //you can customize your cell here because it will be used just for one row.
    }
    cell.textLabel.text = [NSString stringWithFormat:@"I am the %ld Cell ",(long)indexPath.row];
    cell.textLabel.textColor = UIColor.whiteColor;
    cell.backgroundColor = UIColor.lightGrayColor;
    return cell;
}

我已经创建了一个新的测试项目来测试你的场景,但是我无法复制,因为你可以看到 headerview 和 section header 之间没有间隙。

【讨论】:

  • 哈,谢谢!我会看看这个,看看有什么不同:)
  • 好的,告诉我
  • 您对表头视图有什么限制?
  • 正如您从第二张图片中看到的那样,“lbltblHeader”是一个单独的单一视图,我没有对其设置任何约束,您可以告诉我标题视图中包含哪些元素。我会尝试添加它们并给出约束