tableView在項(xiàng)目的開(kāi)發(fā)中使用頻率很高, 所以它的一些代理方法也要很熟悉, 接下來(lái)就講講tableView的分區(qū)頭視圖, 系統(tǒng)給的頭視圖中只有很少的屬性, 而且一般不能滿(mǎn)足我們的需求, 所以我們需要自定義, 雖然tableView有返回View的方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
}
但需要注意的是tableView如果要自定義就要繼承于UITableViewHeaderFooterView
- 創(chuàng)建一個(gè)類(lèi)繼承于UITableViewHeaderFooterView
- 重寫(xiě)
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
// 在這里可以創(chuàng)建自定義View中子視圖,
[self createSubViews];
}
return self;
}
- 注意子控件的frame要在layoutSubViews中寫(xiě)(注意父類(lèi)的調(diào)用)
- (void)layoutSubviews {
[super layoutSubviews];
// 寫(xiě)子控件的frame值
}
- 在VC中調(diào)用實(shí)現(xiàn)以下方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// 這里創(chuàng)建的自定義View在上面tableView要先注冊(cè)
}
看一下效果
看一下具體實(shí)現(xiàn)代碼:
其中Model是一個(gè)數(shù)據(jù)模型
自定義的MyView.h
#import <UIKit/UIKit.h>
@class Model;
@interface MyView : UITableViewHeaderFooterView
@property (nonatomic, strong) Model *model;
@end
自定義的MyView.m
#import "MyView.h"
#import "Model.h"
@interface MyView ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *viewOfBottom;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *imageViewOfMore;
@end
@implementation MyView
// init
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
[self createSubViews];
}
return self;
}
// 創(chuàng)建子視圖
- (void)createSubViews {
self.label = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_label];
self.label.textColor = [UIColor whiteColor];
self.label.textAlignment = NSTextAlignmentCenter;
// _label.backgroundColor = [UIColor redColor];
self.viewOfBottom = [[UIView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_viewOfBottom];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.viewOfBottom addSubview:_button];
// _button.backgroundColor = [UIColor whiteColor];
self.button.tintColor = [UIColor lightGrayColor];
self.button.titleLabel.font = [UIFont systemFontOfSize:13];
self.button.titleLabel.textAlignment = NSTextAlignmentRight;
self.imageViewOfMore = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.viewOfBottom addSubview:_imageViewOfMore];
// _imageViewOfMore.backgroundColor = [UIColor yellowColor];
}
// Layout布局
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat width = CGRectGetWidth(self.contentView.bounds);
CGFloat height = CGRectGetHeight(self.contentView.bounds);
self.label.frame = CGRectMake(width / 4, 10, width / 2, height - 10);
self.viewOfBottom.frame = CGRectMake(width - 80, height / 3, 65, height * 2 / 3);
self.button.frame = CGRectMake(0, 0, CGRectGetWidth(self.viewOfBottom.bounds) * 2 / 3, CGRectGetHeight(self.viewOfBottom.bounds));
self.imageViewOfMore.frame = CGRectMake(CGRectGetWidth(self.button.bounds), 0, CGRectGetWidth(self.viewOfBottom.bounds) / 3, CGRectGetHeight(self.viewOfBottom.bounds));
}
// 模型賦值
- (void)setModel:(Model *)model {
_model = model;
self.label.text = model.name;
if (model.number != 0) {
[self.button setTitle:@"更多" forState:UIControlStateNormal];
self.imageViewOfMore.image = [UIImage imageNamed:@"more"];
}
}
@end
VC.m中創(chuàng)建tableVIew的時(shí)候注冊(cè)
[_tableView registerClass:[MyView class] forHeaderFooterViewReuseIdentifier:@"header"];
/** 區(qū)頭視圖 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
MyView *header = [tableView initWithReuseIdentifier:@"header"];
header.model = self.mArrOfModel[section + 2];
return header;
}
這樣就可以了