美文网首页iOS开发小知识点
自定义tableView的section header/foot

自定义tableView的section header/foot

作者: anny_4243 | 来源:发表于2016-11-29 00:13 被阅读929次

原文:
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];

相关文章

网友评论

    本文标题:自定义tableView的section header/foot

    本文链接:https://www.haomeiwen.com/subject/ggmnpttx.html