在UITableView
上工作,在主表视图和每个节中都有标题。但是,我们在这两个标题之间出现了一个奇怪的差距。我们已经尝试了其他SO答案中给出的解决方案,例如:self.edgesForExtendedLayout
tableView.contentInset
self.automaticallyAdjustsScrollViewInsets = false
但是没有运气!
我整理了一下发生的情况的屏幕截图:
问题是红色和绿色标题之间的差距,任何帮助,我们都很感谢,如果需要的话,很乐意分享更多代码片段。突出显示的区域在视图调试器中匹配,因此我已经确认这不是在任何地方添加额外填充的问题。
一些相关的代码:
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
属性设置 最佳答案
@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之间没有间隙。