如何设置 UITableView 空白空间的 backgroundColor 不同? [英] How to set UITableView empty space's backgroundColor different?
问题描述
这是一个包含 5 个单元格和一个部分的 tableView.拉动tableView刷新时出现蓝色间隙.
Here is a tableView with 5 cells and one section. there is a blue gap when pulling the tableView to refresh.
代码如下:
- (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;
}
为了去掉tableView前面的蓝色缝隙.我更改了 tableView 的 backgroundColor.并遇到了另一个问题.
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.
tableView 的空白区域的 backgroundColor 很重要.怎么设置不一样?
The empty space's backgroundColor of the tableView matters. How to set it different?
代码如下:
- (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;
}
我曾经用前面的代码在 tableView 后面插入一个白色的视图.
I used to insert a white view behind the tableView with the prior code.
有点用,怎么做更好?
推荐答案
尝试将_salesRankTableView
的背景色设置为white
.
Try to set the background color of _salesRankTableView
to white
.
_salesRankTableView.backgroundColor = [UIColor clearColor]; // Newline
_salesRankTableView.mj_header.backgroundColor = [UIColor whiteColor];
更新:
替换下面的tableView
委托方法:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRect.zero)
}
这篇关于如何设置 UITableView 空白空间的 backgroundColor 不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!