Reputation: 56
Working on a UITableView
, with a header in both the main tableview and each section. However, we are getting a weird gap between these two headers. We have tried the solutions given in other SO answers, such as:
self.edgesForExtendedLayout
tableView.contentInset
self.automaticallyAdjustsScrollViewInsets = false
But no luck!
I've put together a screenshot of what's happening:
The issue is the gap between the red and green headers, any help much appreciated, happy to share further snippets of the code if needed. The highlighted areas are matched in the view debugger, so I have confirmed it isn't an issue of extra padding anywhere.
Some relevant code:
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
}
The header set for the table view is a UIView
, set using the tableHeaderView
property
Upvotes: 0
Views: 1327
Reputation: 768
Here is the code snippet
@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;
}
I have created a new test project to testcase ur scenario, but i was not able to replicate as you can see there is no gap between the headerview and section header.
Upvotes: 1