How to set UITableView empty space's backgroundColor different?

Viewed 808

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?

2 Answers

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)
}

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.

Related