-2

I have a tableView which always has a blank space under navigation bar, after I debugged in view hierarchy I found the tableView's size differs from tableViewWrapperView's size. Any idea?enter image description here

enter image description here enter image description here

The following is the code of the tableViewController that I'm having problems with:

#import "BBTCampusInfoTableViewController.h"
#import "BBTCampusInfoManager.h"
#import "BBTCampusInfoTableViewCell.h"
#import <UIImageView+WebCache.h>

@interface BBTCampusInfoTableViewController ()

@end

@implementation BBTCampusInfoTableViewController

extern NSString * campusInfoNotificationName;

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveCampusInfoNotification) name:campusInfoNotificationName object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    //self.tableView.contentInset = UIEdgeInsetsZero;
    //Retrive all campus infos
    [[BBTCampusInfoManager sharedInfoManager] retriveData:@""];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[BBTCampusInfoManager sharedInfoManager].infoArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140.0f;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"infoCell";
    BBTCampusInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        cell = [[BBTCampusInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    NSArray *infoArray = [BBTCampusInfoManager sharedInfoManager].infoArray;

    [cell setCellContentDictionary:infoArray[indexPath.row]];

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;
}

- (void)didReceiveCampusInfoNotification
{
    NSLog(@"Did receive campus info notification");
    [self.tableView reloadData];
}
Leo
  • 492
  • 1
  • 5
  • 15
  • Possible duplicate of [Remove empty space before cells in UITableView](http://stackoverflow.com/questions/18906919/remove-empty-space-before-cells-in-uitableview) –  Jan 26 '16 at 03:52
  • I've read that one but his methods don't work for me. – Leo Jan 26 '16 at 04:39
  • @Caeser since you have apparently tried everything (And nothing works for you somehow), Add the links to the methods you have read and which do not work for you. Otherwise people will keep repeating the usual methods and waste everyone's time. – NSNoob Jan 26 '16 at 07:03

3 Answers3

1

Since you are keep saying it doesnt work, try this, its a kinda ugly workaround, but it works. (see the image)

  • simply just add some UI component before table view -

enter image description here

This will happen if table view (or scrollview) is the only / first sub-view on the container view.

As a solution you can un-tick Adjust Scroll View Insets See below image

Adjust Scroll View Insets

RJE
  • 781
  • 6
  • 14
  • see my updated answer - but this should have worked - may be your autolayouts settings are off – RJE Jan 26 '16 at 06:58
  • In your case you added a tableView in a viewController? I simply dragged a tableViewController. – Leo Jan 26 '16 at 15:56
  • Actually never seen UITableViewController behave like this (normally UITabelView in UIViewController has this issue). Any way can you see this gap in InterfaceBuilder too, or is it just when you run the app on a device. May be try deleting this UItableViewController in IB and drag and drop new UItableViewController. :) – RJE Jan 27 '16 at 03:05
  • This only happened when I run my program on the simulator, it won't appear in the interface builder. I've tried drop a new one several times but makes no sense :( – Leo Jan 27 '16 at 07:55
0

if using interface builder, try:

Select the view controller, Open Attributes inspector, The "Adjust scroll view insets" is on by default.

or programmatically try:

self.automaticallyAdjustsScrollViewInsets = NO;

in the viewdidload

valosip
  • 3,167
  • 1
  • 14
  • 26
0

Did you try adding this code to viewDidload:

self.edgesForExtendedLayout = UIRectEdgeNone;

Or try this code:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.5
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    return UIView(frame: CGRectZero)
}
vien vu
  • 4,277
  • 2
  • 17
  • 30