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;
    }
user2588945
  • 1,681
  • 6
  • 25
  • 38
  • It looks like you are having that white line between every cell. I would recommend setting your cell separator style to none. `self.yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;` – Priyatham51 Oct 29 '13 at 11:21
  • @Priyatham51 it's not the whiteline between the table cells, but the large space (I guess it's grey) between the map and the table. thanks :) – user2588945 Oct 29 '13 at 11:25
  • i guess that is section/table header. – Toseef Khilji Oct 29 '13 at 11:28

3 Answers3

6

Just add this in you ViewDidLoad method

self.automaticallyAdjustsScrollViewInsets = NO;
Slavcho
  • 2,792
  • 3
  • 30
  • 48
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.

David Yee
  • 3,515
  • 25
  • 45
MarekR
  • 2,410
  • 1
  • 14
  • 10
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)];
Ilario
  • 5,979
  • 2
  • 32
  • 46
  • it was this line: UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 140)]; I changed it to: UIView *headerView = [[UIView alloc] init]; and it worked great! thanks!!! – user2588945 Oct 29 '13 at 11:36