3

Here is a tableView with 5 cells and one section. there is a blue gap when pulling the tableView to refresh.

one

here is the code:

- (UITableView *)salesRankTableView{
    if(!_salesRankTableView){
        _salesRankTableView = [[UITableView alloc] initWithFrame: CGRectMake(8, 183, KScreenWidth - 16, KScreenHeight-183-64) style: UITableViewStyleGrouped];

        [_salesRankTableView registerNib: [UINib nibWithNibName: NSStringFromClass(SalesRankTableViewCell.class) bundle:nil] forCellReuseIdentifier: kSalesRankTableViewCell];
        _salesRankTableView.layer.contents = (__bridge id _Nullable)([UIImage imageNamed: @"blue"].CGImage);

        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget: self refreshingAction: @selector(rankRefresh)];
        header.layer.contents = (__bridge id _Nullable)([UIImage imageNamed: @"white"].CGImage);
        _salesRankTableView.mj_header = header;
    }

    return _salesRankTableView;

}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}


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


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

In order to get rid of the blue gap in the front of the tableView. I changed the tableView's backgroundColor. And met an other question.

2

The empty space's backgroundColor of the tableView matters. How to set it different?

Here is the code:

- (UITableView *)salesRankTableView{
    if(!_salesRankTableView){
        _salesRankTableView = [[UITableView alloc] initWithFrame: CGRectMake(8, 183, KScreenWidth - 16, KScreenHeight-183-64) style: UITableViewStyleGrouped];

        [_salesRankTableView registerNib: [UINib nibWithNibName: NSStringFromClass(SalesRankTableViewCell.class) bundle:nil] forCellReuseIdentifier: kSalesRankTableViewCell];
        _salesRankTableView.layer.contents = (__bridge id _Nullable)([UIImage imageNamed: @"white"].CGImage);

        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget: self refreshingAction: @selector(rankRefresh)];
        header.layer.contents = (__bridge id _Nullable)([UIImage imageNamed: @"white"].CGImage);
        _salesRankTableView.mj_header = header;
    }

    return _salesRankTableView;

}

I used to insert a white view behind the tableView with the prior code.

It is a little wield , How to do it better?

black_pearl
  • 2,549
  • 1
  • 23
  • 36
  • 1
    Probably the simplest solution is to keep the white background but to also observe `contentSize` and change height of the table view accordingly. – Sulthan Nov 02 '18 at 07:51
  • u have take two section or two different cell. without know about no of section and no of row cell no no one understand yur exact problem . see if u have take two section than heightForHeaderInSection and heightForFooterInSection return 0. – Krishna kushwaha Nov 02 '18 at 08:13
  • @Krishna kushwaha, just one section, and one kind of cell – black_pearl Nov 02 '18 at 08:18

2 Answers2

0

Try to set the background color of _salesRankTableView to white.

_salesRankTableView.backgroundColor = [UIColor clearColor]; // Newline
_salesRankTableView.mj_header.backgroundColor = [UIColor whiteColor];

Update:

Replace tableView delegate methods below:

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    return 0
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    return UIView(frame: CGRect.zero)
}
Sateesh Yemireddi
  • 4,289
  • 1
  • 20
  • 37
0

You can make it by typing a single line of code in viewDidLoad()

 self.tableView.tableFooterView = UIView()

here the free spaced cells will be removed and if you wish to change the tableview color to some other you can make it by changing the background color of the tableView.If you want the color of superview background as the free space you should change the tableview background color to 'clear'.

 self.tableView.backgroundColor = UIColor.clear

Hope it will help.

Fansad PP
  • 459
  • 4
  • 11
  • Thank you Fansad. This is the perfect Solution –  Nov 02 '18 at 09:53
  • Not fit for my situation. That goes the upper situation. In fact , I use https://github.com/CoderMJLee/MJRefresh. I think the answer is to do some change to the repo. And Sulthan's suggestion fits more. – black_pearl Nov 02 '18 at 10:02