【问题标题】:Spacing between UITableview header and top cellUITableview 标题和顶部单元格之间的间距
【发布时间】:2015-06-02 20:27:57
【问题描述】:

我的 UITableView 出现了一些奇怪的行为。

我的 UITableView 标题和第一个单元格之间有间距。知道这是为什么吗?

#define kDefaultTableViewSectionHeight 50

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.posts.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDefaultTableViewCellHeight;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDefaultTableViewSectionHeight)];
    UIButton *hot = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 0, 100, kDefaultTableViewSectionHeight)];
    UIButton *new = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100, 0, 100, kDefaultTableViewSectionHeight)];
    UIView *underline = [[UIView alloc] initWithFrame:CGRectMake(new.frame.origin.x, view.frame.size.height-10, 70, 1)];
    UIView *border = [[UIView alloc] initWithFrame:CGRectMake(0, view.frame.size.height-1, view.frame.size.width, 1)];

    [border setBackgroundColor:[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1]];

    self.underline = underline;

    if (self.new)
        [underline setCenter:CGPointMake(new.center.x, underline.center.y)];
    else
        [underline setCenter:CGPointMake(hot.center.x, underline.center.y)];

    [underline setBackgroundColor:[UIColor whiteColor]];
    [view setBackgroundColor:[UIColor blackColor]];

    [hot setTitle:@"Hot" forState:UIControlStateNormal];
    [hot setTag:2];
    [hot setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [hot addTarget:self action:@selector(selectQueryType:) forControlEvents:UIControlEventTouchUpInside];
    [hot.titleLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:20.0]];

    [new setTitle:@"New" forState:UIControlStateNormal];
    [new setTag:1];
    [new setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [new addTarget:self action:@selector(selectQueryType:) forControlEvents:UIControlEventTouchUpInside];
    [new.titleLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:20.0]];

    [view addSubview:new];
    [view addSubview:hot];
    [view addSubview:underline];
    [view addSubview:border];
    return view;
}

我尝试了以下代码来解决这个问题,但这只是减少了 UITableView 顶部和 UINavigationBar 之间的空间:

    /*
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
        self.postTableview.contentInset = UIEdgeInsetsMake(-8, 0, 0, 0);
    } */

但这只是减少了 UITableView 顶部和 UINavigationbar 之间的间距。

【问题讨论】:

  • 这里你有另一个问题,你可能会找到这个stackoverflow.com/questions/18880341/…的答案
  • @ClaudioRedi 我的问题是不同的,因为间距 starts 在标题和 ends 在第一个单元格的顶部。
  • 其中一个答案是关于为节标题设置 0。我建议您深入了解一下,因为有很多不同的答案。
  • 您是否使用委托来设置 headerView?你检查过返回的高度:- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section? kDefaultTableViewSectionHeight *50* 从图像中我认为它在 35 左右或者尝试向kDefaultTableViewSectionHeight 声明较小的值
  • @0yeoj 是的,它在我上面问题的代码中

标签: ios objective-c uitableview


【解决方案1】:

我认为您将tableHeaderView 属性与部分标题视图混淆了。您正在调用的委托方法tableview:viewForHeaderInSection: 设置节的标题,而不是UITableView 本身的标题。在您的-numberOfSectionsInTableView: 中,您只返回一个部分,因此您只需将自定义标题添加为表格视图的标题,而不是作为表格视图中第一部分的标题。这并不能解决您的具体问题,但考虑到表格视图中只有 1 个部分,这似乎是您的应用程序的更好解决方案。

-viewDidLoad 中,您只需将tableHeaderView 属性设置为您的自定义视图,如下所示:

- (void)viewDidLoad{

  //...

  _tableview.tableHeaderView = [self myTableHeader];

}

- (UIView *)myTableHeader{

  //Return custom header view 

}

【讨论】:

  • 我想让标题在您滚动时保持在顶部
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多