Reputation: 1681
I'm have been trying to get the header of the table scrolling along with the UITableView
. 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.
in my viewDidLoad
: 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 viewForHeaderInSection:
method (It was suggested that this be done in the viewDidLoad:
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
action:@selector(mapDetailViewDisplay:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:self.topMapView];
[headerView showMapButton];
self.tableView.tableHeaderView = headerView;
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 140;
}
Upvotes: 0
Views: 1773
Reputation: 2410
[UITableView tableHeaderView]
is something different than viewForHeaderInSection
but you are setting the same object to both of them.
You should rather set self.tableView.tableHeaderView
in viewDidLoad:
or return a headerView through viewForHeaderInSection delegate method depending on what kind of behaviour you are trying to achieve.
Upvotes: 2
Reputation: 2812
Just add this in you ViewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
Upvotes: 6
Reputation: 6079
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)];
Upvotes: 1