在项目开发中,如果要做到iOS 6 和iOS 7 之后版本的适配, tablview的变化无疑是最多的。
我们知道 iOS 6 和 iOS 7 或者更高的版本之间,改变最大的就是group样式。要做到iOS 6 之前的表格样式也类似于扁平化的样式
我们首选的方案就是使用tablview 的plain样式。
使用此方法设置section的表头标题的话,header 是不会随着section一起移动的
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
那么如何做才能解决tableview的plain样式,使它如group样式 下,header 随着对应的section一起移动的呢?
我解决的方案如下:(只做参考,网友们有更好的方案可以回复,大家一起交流)
在tableview的代理方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
自定义header。
iOS 6.0 之后,apple添加了UITableViewHeaderFooterView控件。要是只适配6.0以及更高的版本可以采用以下做法
自定义一个继承于UITableViewHeaderFooterView的 HeaderView
#import <UIKit/UIKit.h>
@interface HeaderView : UITableViewHeaderFooterView
{
UITableView *tableView;
}
/**
* 对应section
*/
@property (nonatomic ,assign) NSInteger section;
/**
* 类方法,headerView的复用
*/
+ (instancetype)headerViewWithTableView:(UITableView *)tableView;
@end
实现文件.m
#import "HeaderView.h"
@implementation HeaderView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
static NSString *headIdentifier = @"header";
HeaderView *headerView = [tableView dequeueReusableCellWithIdentifier:headIdentifier];
if (headerView == nil) {
headerView = [[HeaderView alloc] initWithReuseIdentifier:headIdentifier];
headerView->tableView = tableView;
}
return headerView;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
// 初始化控件 可以在 UITableViewHeaderFooterView的基础上自定义自己的控件
}
return self;
}
/**
* 重置header的frame,关键代码
*/
- (void)setFrame:(CGRect)frame
{
CGRect sectionRect = [self->tableView rectForHeaderInSection:self.section];
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame));
[super setFrame:newFrame];
}
@end
在tableViewController控制器中使用
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
HeaderView *header = [HeaderView headerViewWithTableView:tableView];
header.section = section;
// 这里只简单了文字
header.textLabel.text = @"浮动";
return header;
}
如果你还苦逼着要适配6.0之前的项目的话,header建议继承UIView,做法如下:
#import <UIKit/UIKit.h>
@interface HeaderView : UIView
/** section*/
@property NSUInteger section;
/** tableView*/
@property (nonatomic, weak) UITableView *tableView;
/** 文字 */
@property (nonatomic ,copy) NSString *title;
@end
实现文件.m
#import "HeaderView.h"
@interface HeaderView()
@property (nonatomic ,weak) UILabel *contentTitle;
@end
@implementation HeaderView
- (id)init
{
if (self = [super init]) {
self.backgroundColor = [UIColor clearColor];
UILabel *contentTitle = [[UILabel alloc] init];
[self addSubview:contentTitle];
self.contentTitle = contentTitle;
}
return self;
}
- (void)setFrame:(CGRect)frame
{
CGRect sectionRect = [self.tableView rectForSection:self.section];
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame));
[super setFrame:newFrame];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.contentTitle.frame = self.bounds;
}
- (void)setTitle:(NSString *)title
{
_title = title;
self.contentTitle.text = title;
}
@end
控制器中的实现代码如下
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
HeaderView *header = [[HeaderView alloc] init];
header.section = section;
header.tableView = tableView;
header.title = @"浮动";
return header;
}