[英]space between header and table cell
我想增加UITableView的标题和单元格之间的差距。
我搜索了很多,我找到了这个解决方案,
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Adding UIView and it's Background Image...
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
UIImageView *BgImg=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,20)];
BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
[tempView addSubview:BgImg];
//Adding UIImageView...
UIImageView *flag=[[UIImageView alloc]initWithFrame:CGRectMake(10,3,20,12)];
flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
[tempView addSubview:flag];
//Adding UILabel...
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(40,1,300,15)];
tempLabel.backgroundColor=[UIColor colorWithRed:15.0/255.0 green:15.0/255.0 blue:30.0/255.0 alpha:1.0];
tempLabel.textColor=[UIColor whiteColor];
tempLabel.font=[UIFont systemFontOfSize:11];
tempLabel.text=@"United Kingdom";
[tempView addSubview: tempLabel];
return tempView;
}
我试过这个代码,它在模拟器中工作正常,但是当我在我的设备上运行它时它没有显示任何空格。
这是模拟器的快照:(我可以看到标题和单元格之间的差距..) : 提前谢谢。
尝试这个:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50.0;
}
那么你可能面临的问题是你正在使用iphone 5设备和你为iphone 4运行模拟器,反之亦然。 您也可以启用自动布局。 同时检查将heightForHeaderInSection
的高度增加到50.0或更高,因为设备中的高度无论如何都看起来很小。
我认为问题是你正在使用图像来加载标题视图。 当您在iPhone 4s中运行代码时,这是视网膜显示屏手机。 在代码下面添加所有图像的@ 2x图像非常重要:
BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
请添加@ 2x图像并尝试。
Swift 4.2
1-创建自定义标题类,为其提供清晰的背景。
class CustomHeader: UITableViewHeaderFooterView{
override func awakeFromNib() {
super.awakeFromNib()
backgroundView = UIView(frame: self.bounds)
backgroundView!.backgroundColor = .clear
}
}
2-创建.xib文件并指定刚刚创建的类。
3-在.xib文件中的自定义标头内创建背景视图
4-为此背景视图提供底部填充,并将其表现为标题视图子视图的容器。
5-不要忘记将自定义标题注册到您的tableview。
tableView.register(UINib(nibName: "NibName", bundle: nil), forHeaderFooterViewReuseIdentifier: "Identifier")
6-随时使用标题。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader
// Customize your header
return header
}
7-如果您不需要自定义标题并且只使用一个自定义标题,则可以在设置表格视图的地方使用此快捷方式。
yourTableView.tableHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.