【发布时间】: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