iOS开发小知识点

自定义tableView的section header/foot

2016-11-29  本文已影响929人  anny_4243

原文:
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];
上一篇 下一篇

猜你喜欢

热点阅读