原文:
http://blog.csdn.net/sky_yang1024/article/details/51273057
1.首先要自定義一個sectionHeadView/sectionFootView繼承自 UITableViewHeaderFooterView,如下:
@interface FriendCircleView : UITableViewHeaderFooterView
2.在自定義的sectionHeadView/sectionFootView中重寫這個方法,設置復用
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
[self _init];//_init表示初始化方法
}
return self;
}
3.在需要調用自定義sectionHeadView/sectionFootView的VC里面調用table的代理方法,用法跟cell的復用相似
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *viewIdentfier = @"headView";
FriendCircleView *sectionHeadView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:viewIdentfier];
if(!sectionHeadView){
sectionHeadView = [[FriendCircleView alloc] initWithReuseIdentifier:viewIdentfier];
}
sectionHeadView.friendCircleModel = _postArray[section];
return sectionHeadView;
}
4.若想改變自定義區頭的背景色,需設置:
self.contentView.backgroundColor = [UIColor whiteColor];