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];