如何删除UITableView上方的额外空格?

时间:2013-10-29 11:13:29

标签: ios iphone objective-c uitableview

我一直试图让表格的标题与UITableView一起滚动。它终于工作了,但是现在在地图视图下方显示了一个空白(见截图)。我很难在表格标题部分中获取地图以滚动表格,并且还通过用户触摸使UIButton在表格标题内触发(UIButton只是将地图扩展为全屏幕)。

昨天我问了一个与此相关的问题,关于滚动和按钮触摸。那些现在正在运行 - 我确信编码可以做得更好,但这就是我现在所拥有的,并且必须尽快完成。如果我可以解决这个空白问题,我可以稍后重构和优化代码。

感谢您的帮助。

注意地图下方和屏幕截图中的表格视图上方的空白区域 - 我想带来表格视图,使其接近地图。

enter image description here

在我的viewDidLoad:方法

  // 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];

然后在viewForHeaderInSection:方法中(建议在viewDidLoad:方法中完成此操作,我尝试了但是它给了我更多问题

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

3 个答案:

答案 0 :(得分:6)

只需在ViewDidLoad方法

中添加此内容即可
self.automaticallyAdjustsScrollViewInsets = NO;

答案 1 :(得分:2)

[UITableView tableHeaderView]viewForHeaderInSection不同,但您要为它们设置相同的对象。

您应该在self.tableView.tableHeaderView中设置viewDidLoad:或者通过viewForHeaderInSection委托方法返回headerView,具体取决于您尝试实现的行为类型。

答案 2 :(得分:1)

尝试移动- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section中的所有代码,只留下return headerView

viewDidLoad

中的

并更改

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

而不是

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 140)];