user2588945 user2588945 - 2 years ago 121
Objective-C Question

How to remove extra whitespace above a UITableView?

I'm have been trying to get the header of the table scrolling along with the

. It's finally working, however, now there's a whitespace showing up right under the map view (see screenshot). I had difficulty getting the map in the table header section to scroll with the table and also to get the UIButton to trigger within the table header with a user touch (the UIButton simply expands the map to full screen.).

I asked a question related to this yesterday regarding the scrolling and button touch. Those are working now - I'm sure the coding could be done better, but this is what I have at the moment and have to get this done asap. If I can resolve this whitespace issue I can refactor and optimize the code later.

thanks for any help with this.

notice the whitespace under the map and above the table view in the screenshot - I want to bring the table view so it's close to the map.

enter image description here

in my
: method

// mapview
self.topMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.height,self.view.frame.size.height)];

self.tableView.dataSource = self;
self.tableView.delegate = self;

self.tableView.backgroundColor = [UIColor whiteColor];

[self.view addSubview:self.tableView];

then in the
method (It was suggested that this be done in the
method and I tried that but it gave me more problems

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

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 140)];
UIButton *showMapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showMapButton.frame = CGRectMake(0.0, 0, 320.0, 140.0);
[showMapButton setTitle:@"show map" forState:UIControlStateNormal];
[showMapButton addTarget:self

[headerView addSubview:self.topMapView];
[headerView showMapButton];

self.tableView.tableHeaderView = headerView;

return headerView;

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

Answer Source

try to move all code in - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section leaving only return headerView

in viewDidLoad

and change

UIView *headerView = [[UIView alloc] init];  

instead of

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 140)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download