【问题标题】:space between header and table cell标题和表格单元格之间的空间
【发布时间】:2013-07-30 10:25:30
【问题描述】:

我想增加 UITableView 的 header 和 cell 之间的差距。

我搜索了很多,我找到了这个解决方案,

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

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   //Adding UIView and it's Background Image...
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
    UIImageView *BgImg=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,20)];
    BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
    [tempView addSubview:BgImg];

    //Adding UIImageView...
    UIImageView *flag=[[UIImageView alloc]initWithFrame:CGRectMake(10,3,20,12)];
    flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
    [tempView addSubview:flag];

    //Adding UILabel...
    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(40,1,300,15)];
    tempLabel.backgroundColor=[UIColor colorWithRed:15.0/255.0 green:15.0/255.0 blue:30.0/255.0 alpha:1.0];
    tempLabel.textColor=[UIColor whiteColor];
    tempLabel.font=[UIFont systemFontOfSize:11];
    tempLabel.text=@"United Kingdom";
    [tempView addSubview: tempLabel];

    return tempView;
}

我试过这段代码,它在模拟器中运行良好,但没有显示任何空格, 当我在我的设备上运行它时。

这是模拟器的快照:(我可以看到 Header 和单元格之间的间隙..) : 提前致谢。

【问题讨论】:

  • 您使用的是哪种设备?
  • @krunal 增加你的 'heightForHeaderInSection' 高度,比如返回 60.0。
  • 您使用的是哪个设备和模拟器...是指 4 英寸还是 3.5 英寸?
  • 3.5" 模拟器和设备iphone 4s

标签: iphone ios ipad uitableview


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    那么您可能面临的问题是您使用的是 iphone 5 设备并且您正在运行 iphone 4 的模拟器,反之亦然。你也可能有你的自动布局。还要检查以将heightForHeaderInSection 的高度增加到 50.0 或更高,因为无论如何设备中的高度看起来都很小。

    【讨论】:

      【解决方案3】:

      我认为问题在于您正在使用图像来加载标题视图。当您在视网膜显示手机 iPhone 4s 中运行代码时。在代码下方添加您使用过的所有图像的@2x 图像,这一点很重要:

      BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
      flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
      

      请添加@2x 图片并尝试。​​

      【讨论】:

        【解决方案4】:

        斯威夫特 4.2

        1-创建一个自定义标题类,给它一个清晰的背景。

        class CustomHeader: UITableViewHeaderFooterView{
            override func awakeFromNib() {
                super.awakeFromNib()
                backgroundView = UIView(frame: self.bounds)
                backgroundView!.backgroundColor = .clear
            }
        } 
        

        2- 创建一个 .xib 文件并分配您刚刚创建的类。

        3- 在 .xib 文件的自定义标头内创建背景视图

        4-为此背景视图提供底部填充,并将其作为标题视图子视图的容器。

        5-不要忘记将您的自定义标题注册到您的表格视图。

        tableView.register(UINib(nibName: "NibName", bundle: nil), forHeaderFooterViewReuseIdentifier: "Identifier")
        

        6-随时使用标题。

        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader
            // Customize your header
            return header
        }
        

        7-如果您不需要自定义标题并且只使用一个自定义标题,则可以在设置表格视图的位置使用此快捷方式。

        yourTableView.tableHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader 
        

        【讨论】: