0

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.

enter image description here

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;
    }
4

3 回答 3

6

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = NO;
于 2014-11-13T14:59:57.340 回答
2

[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.

于 2013-10-29T11:31:04.380 回答
1

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)];
于 2013-10-29T11:27:00.713 回答