iOS --tableView的组头/组尾

需求:表头上显示文字

  • 两种方案:系统给我们提供了tableViewController的代理方法
    • 返回组头/组尾文字字符串 NSString
- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
  • 返回组头/尾视图,UIView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height

方案一:

  • 实现代理方法 -> table: titleForHeaderInSection: ,直接返回表头字符串即可。
- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;  

方案二:自定义表头视图 实现

  • 创建自定义tableView类 JPCommentHeaderView

  • 继承UITableViewHeaderFooterView,因为系统提供的UITableViewHeaderFooterView类有重用机制

  • 提供一个文字属性该外界设置

  • 代码实现

 #import <UIKit/UIKit.h>
@interface JPCommentHeaderView : UITableViewHeaderFooterView
/** 文字属性 */
@property (nonatomic, copy) NSString *text;
@end

#import "JPCommentHeaderView.h"

@interface JPCommentHeaderView()
/** 内部的label */
@property (nonatomic, weak) UILabel *label;
@end

@implementation JPCommentHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = XMGCommonBgColor;
        
        // label
        UILabel *label = [[UILabel alloc] init];
        label.x = XMGCommonSmallMargin;
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        label.textColor = XMGGrayColor(120);
        label.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:label];
        self.label = label;
    }
    return self;
}

- (void)setText:(NSString *)text
{
    _text = [text copy];
    self.label.text = text;
}
@end
  • 注册自定义组头视图
   static NSString * const JPHeaderId = @"header";
   [self.tableView registerClass:[JPCommentHeaderView class] forHeaderFooterViewReuseIdentifier:JPHeaderId];
  • 实现代理方法,返回组头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    JPCommentHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:JPHeaderId
                                           ];
    // 覆盖文字
    if (section == 0 && self.hotComments.count) {
        header.text = @"最热评论";
    } else {
        header.text = @"最新评论";
    }
    return header;
}
tableView 头部有空白区域解决
当cell的类型是plaint类型时,直接设置self.automaticallyAdjustsScrollViewInsets=NO;应该就可以的
当cell的类型是group类型时,此时要去掉tableView顶部的空白需要两步:
1.设置tableView的tableHeaderView高度为0.5;
self.MenuTable.tableHeaderView=[[UIview alloc] initWithFrame:(CGRectMake(0,20,82,0.5))];
2.设置heightForHeaderInSection的高度为0.5
-(CGFloat)tableView:(UItableView *)tableView heightForHeaderInSection(NSInteger)section{
return 0.5;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,647评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 举起笔勾勒,放下纸沉思—— 心似扇窗,敞开即是阳光。然而有时心似一片湖水,平静地出奇,白昼如同一页...
    暮玦阅读 226评论 3 0
  • 两天后,我就像完成了一场战役。可是无论好坏,我都不恋战了。因为我太累了,或者说我觉得我太累了。如果你的心已经无法给...
    InkInk阅读 202评论 0 1