一个关于tableView的footerView的子控件y 坐标的 BUG
来源:互联网 发布:人体润滑液 知乎 编辑:程序博客网 时间:2024/06/04 01:37
根据 距离 屏幕底边的距离 设置 控件的 y 坐标:
eg.
- Bug 内容:
在这个 viewController 上创建 tableView 时,不应该设置为分组样式:
UITableView * tableView = [[UITableView alloc] initWithFrame:[UIScreenmainScreen].bounds style:UITableViewStyleGrouped];
应该设置为:
UITableView * tableView = [[UITableView alloc] initWithFrame:[UIScreenmainScreen].bounds ];
然后再设置 tableView 的背景颜色;
Bug 原因:
设置为分组样式会导致出现组与组之间的默认距离,并且无法修改.
- 设置组的 header/footer 高度 :
#pragma mark - 组的 Header 高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return0;
}
#pragma mark - 组的 footer 的高度-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return0;
}
- 设置 footerView 的 frame:
_footerView.frame = CGRectMake(0, 0, KWIDTH,KHEIGHT - _headerView.height - 44 - 64);
注意:footerView/headerView 都是紧贴cell的,所以y值无作用;
- 添加控件:
添加时,根据高度计算出 y 坐标(坐标是相对于 footerView 的 y 坐标,而不是整个屏幕的 y 坐标):
versionLabel.y = _footerView.height - 56;
附:
1.整个类文件代码:
#import"ZJHAboutViewController.h"
#import "ZJHUserProtocolViewController.h"
#import "UIView+Extension.h"
#import "UIColor+Helpers.h"
#define kWIDTH [UIScreen mainScreen].bounds.size.width
@interface ZJHAboutViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)UIView* headerView;
@property (strong,nonatomic)UIView* footerView;
@end
@implementation ZJHAboutViewController
#pragma mark - 关于产品类
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// nav 的左侧按钮 和 标题
UIBarButtonItem*leftItem = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"product_button_back_nor.png"]style:UIBarButtonItemStylePlaintarget:nilaction:nil];
self.navigationItem.leftBarButtonItem= leftItem;
self.navigationItem.title=@"NavigationController";
// 创建 tableView
UITableView* tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].bounds];// style:UITableViewStyleGrouped
tableView.backgroundColor= [UIColorcolorWithRed:(float)(242/250.0)green:(float)(242/250.0)blue:(float)(242/250.0)alpha:1];
tableView.delegate=self;
tableView.dataSource=self;
tableView.tableHeaderView=self.headerView;
tableView.tableFooterView=self.footerView;
// tableView.scrollEnabled = NO;
[self.viewaddSubview:tableView];
}
#pragma mark - 懒加载 headerView
-(UIView*)headerView{
if (!_headerView) {
_headerView= [[UIViewalloc]init];
// 创建 logo 图片
UIImageView* logoImageView = [[UIImageViewalloc]init];
logoImageView.backgroundColor= [UIColorcolorWithRed:((float)arc4random_uniform(256)/255.0)green:((float)arc4random_uniform(256)/255.0)blue:((float)arc4random_uniform(256)/255.0)alpha:1];
logoImageView.centerX= (375-150) *0.5;
logoImageView.y=30;
logoImageView.width=150;
logoImageView.height=150;
// 设置 HeaderView 大小
_headerView.frame=CGRectMake(0,0,kWIDTH, logoImageView.y+ logoImageView.height+30);
NSLog(@"%f",_headerView.height);
// 添加 logo
[_headerViewaddSubview:logoImageView];
}
return _headerView;
}
#pragma mark - 懒加载 footerView
- (UIView*)footerView{
if (!_footerView) {
_footerView=[[UIViewalloc]init];
_footerView.frame=CGRectMake(0,0,KWIDTH,KHEIGHT-_headerView.height-44- 64);
// _footerView.backgroundColor = [UIColor redColor];
// 用户交流 QQ 群
UILabel* userLabel = [[UILabelalloc]init];
userLabel.text=@"DDDDDD:";
userLabel.font= [UIFontsystemFontOfSize:18];
userLabel.textColor= [UIColorcolorWithHexString:@"#c7c7c7"];
[userLabel sizeToFit];
UILabel* QQLabel = [[UILabelalloc]init];
QQLabel.text=@"QQXXXXXXXXXX";
QQLabel.font= [UIFontsystemFontOfSize:18];
QQLabel.textColor= [UIColorcolorWithHexString:@"#07c0c0"];
[QQLabel sizeToFit];
userLabel.x= (KWIDTH- userLabel.width- QQLabel.width) *0.5;
QQLabel.x= userLabel.x+ userLabel.width;
userLabel.y=_footerView.height-88;
QQLabel.y=_footerView.height-88;
// 版本号
// 创建爱心标语
UILabel* versionLabel = [[UILabelalloc]init];
versionLabel.text=@"Xxxxxxx 222222";
versionLabel.textColor= [UIColorcolorWithHexString:@"#c7c7c7"];
versionLabel.textAlignment=NSTextAlignmentCenter;
versionLabel.font= [UIFontsystemFontOfSize:18];
[versionLabel sizeToFit];
versionLabel.centerX=KWIDTH* 0.5;
versionLabel.y=_footerView.height-56;
[_footerViewaddSubview:userLabel];
[_footerViewaddSubview:versionLabel];
[_footerViewaddSubview:QQLabel];
}
return _footerView;
}
#pragma mark - UItableView的数据源代理方法
// 一共多少行
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
// 每行长啥样
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString * aboutKey = @"aboutKey";
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:aboutKey];
if (!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:aboutKey];
}
cell.textLabel.text=@"XXXXXX";
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor= [UIColorwhiteColor];
return cell;
}
#pragma mark - 点击 cell 跳转至下一个控制器
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
ZJHUserProtocolViewController* ptl = [[ZJHUserProtocolViewControlleralloc]init];
[self.navigationControllerpushViewController:ptlanimated:YES];
}
#pragma mark - 组的 Header 高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
return 0;
}
#pragma mark - 组的 footer 的高度
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
#import "ZJHUserProtocolViewController.h"
#import "UIView+Extension.h"
#import "UIColor+Helpers.h"
#define kWIDTH [UIScreen mainScreen].bounds.size.width
@interface ZJHAboutViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)UIView* headerView;
@property (strong,nonatomic)UIView* footerView;
@end
@implementation ZJHAboutViewController
#pragma mark - 关于产品类
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// nav 的左侧按钮 和 标题
UIBarButtonItem*leftItem = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"product_button_back_nor.png"]style:UIBarButtonItemStylePlaintarget:nilaction:nil];
self.navigationItem.leftBarButtonItem= leftItem;
self.navigationItem.title=@"NavigationController";
// 创建 tableView
UITableView* tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].bounds];// style:UITableViewStyleGrouped
tableView.backgroundColor= [UIColorcolorWithRed:(float)(242/250.0)green:(float)(242/250.0)blue:(float)(242/250.0)alpha:1];
tableView.delegate=self;
tableView.dataSource=self;
tableView.tableHeaderView=self.headerView;
tableView.tableFooterView=self.footerView;
// tableView.scrollEnabled = NO;
[self.viewaddSubview:tableView];
}
#pragma mark - 懒加载 headerView
-(UIView*)headerView{
if (!_headerView) {
_headerView= [[UIViewalloc]init];
// 创建 logo 图片
UIImageView* logoImageView = [[UIImageViewalloc]init];
logoImageView.backgroundColor= [UIColorcolorWithRed:((float)arc4random_uniform(256)/255.0)green:((float)arc4random_uniform(256)/255.0)blue:((float)arc4random_uniform(256)/255.0)alpha:1];
logoImageView.centerX= (375-150) *0.5;
logoImageView.y=30;
logoImageView.width=150;
logoImageView.height=150;
// 设置 HeaderView 大小
_headerView.frame=CGRectMake(0,0,kWIDTH, logoImageView.y+ logoImageView.height+30);
NSLog(@"%f",_headerView.height);
// 添加 logo
[_headerViewaddSubview:logoImageView];
}
return _headerView;
}
#pragma mark - 懒加载 footerView
- (UIView*)footerView{
if (!_footerView) {
_footerView=[[UIViewalloc]init];
_footerView.frame=CGRectMake(0,0,KWIDTH,KHEIGHT-_headerView.height-44- 64);
// _footerView.backgroundColor = [UIColor redColor];
// 用户交流 QQ 群
UILabel* userLabel = [[UILabelalloc]init];
userLabel.text=@"DDDDDD:";
userLabel.font= [UIFontsystemFontOfSize:18];
userLabel.textColor= [UIColorcolorWithHexString:@"#c7c7c7"];
[userLabel sizeToFit];
UILabel* QQLabel = [[UILabelalloc]init];
QQLabel.text=@"QQXXXXXXXXXX";
QQLabel.font= [UIFontsystemFontOfSize:18];
QQLabel.textColor= [UIColorcolorWithHexString:@"#07c0c0"];
[QQLabel sizeToFit];
userLabel.x= (KWIDTH- userLabel.width- QQLabel.width) *0.5;
QQLabel.x= userLabel.x+ userLabel.width;
userLabel.y=_footerView.height-88;
QQLabel.y=_footerView.height-88;
// 版本号
// 创建爱心标语
UILabel* versionLabel = [[UILabelalloc]init];
versionLabel.text=@"Xxxxxxx 222222";
versionLabel.textColor= [UIColorcolorWithHexString:@"#c7c7c7"];
versionLabel.textAlignment=NSTextAlignmentCenter;
versionLabel.font= [UIFontsystemFontOfSize:18];
[versionLabel sizeToFit];
versionLabel.centerX=KWIDTH* 0.5;
versionLabel.y=_footerView.height-56;
[_footerViewaddSubview:userLabel];
[_footerViewaddSubview:versionLabel];
[_footerViewaddSubview:QQLabel];
}
return _footerView;
}
#pragma mark - UItableView的数据源代理方法
// 一共多少行
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
// 每行长啥样
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString * aboutKey = @"aboutKey";
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:aboutKey];
if (!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:aboutKey];
}
cell.textLabel.text=@"XXXXXX";
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor= [UIColorwhiteColor];
return cell;
}
#pragma mark - 点击 cell 跳转至下一个控制器
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
ZJHUserProtocolViewController* ptl = [[ZJHUserProtocolViewControlleralloc]init];
[self.navigationControllerpushViewController:ptlanimated:YES];
}
#pragma mark - 组的 Header 高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
return 0;
}
#pragma mark - 组的 footer 的高度
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
@end
2.分类:
设置控件xy长宽分类:#import "UIView+Extension.h"
根据十六进制设置颜色分类:#import "UIColor+Helpers.h"
0 0
- 一个关于tableView的footerView的子控件y 坐标的 BUG
- 自定义tableview的footerview
- tableview的headerview和footerview的做法
- 关于tableview的一些bug
- 关于在tableview的footerView上添加button的点击不响应事件
- 取消TableView的headerView和footerView的悬停效果
- iOS storyboard中设置TableView的headerview 和 footerview
- 获取view的y坐标
- 关于Android的GridView添加HeaderView,FooterView.
- 关于UITableView的headerView和footerView
- 【iOS】TableView的footerView不随cell滚动而停留在tableView底部的问题
- TableView的footerView不随cell滚动而停留在tableView底部的问题,ios tableviewfooter
- tableView的每个section 的headerView或者footerView随tableView一起滚动
- 关于vs2010中Chart控件的一个小BUG
- 关于display的y一个小细节
- 关于解决这个问题:从一个点(x,y)绕另一个点(x0,y0)旋转任意角度A后得到的坐标(x1,y1)的坐标是多少?
- 关于tableview高度自适应的一个方法
- 关于tableview 的一个小问题
- 吾将上下而求索 VB 小小小
- 删除一种文件类型的文件
- Spring注解@Resource和@Autowired的区别对比
- javaSE_包装类
- 汇编语言:从键盘输入十进制数,输出该十进制数的十进制,二进制(B),十六进制(H),八进制(Q)表示
- 一个关于tableView的footerView的子控件y 坐标的 BUG
- javaSE_File,RandomAccessFile,FileFilter
- 我和阿毛的点点滴滴
- LeetCode 240. Search a 2D Matrix II (矩阵内搜索)
- 重学数据结构系列之——图的遍历(广度优先搜索和深度优先搜索)学习来源:计蒜客
- springMVC4(15)RestFul多视图混合输出
- Unity Shaders and Effects Cookbook (3-2) 创建简单的 Phong 高光 光照模型
- [Android]IPC通信之Messenger的使用方法介绍
- iOS JSON与Object之间的映射 ObjectMapper For Swift