iOS开发自定义页脚和页眉技巧详解

 更新时间:2022年07月26日 14:10:03   作者:公众号iOS逆向  
这篇文章主要为大家介绍了iOS开发自定义页脚和页眉的技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

应用场景:

  • 商品管理列表的页眉显示商品数量和批量操作

  • 修改界面对密码规则的说明

I 自定义页脚和页眉

如果系统的APItitleForHeaderInSection 满足不了你的需求,可以自定义UITableViewHeaderFooterView。

1.1 自定义分组页眉的步骤

自定义UITableViewHeaderFooterView的步骤:

  • 注册
        [_tableView registerClass:[CRMPasswordRuleDescHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"CRMPasswordRuleDescHeaderFooterView"];
  • 创建
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CRMPasswordRuleDescHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CRMPasswordRuleDescHeaderFooterView"];
//    footerView.type = self.type;
    footerView.models = self.viewModel.passwordRuleDescModel;
    return footerView;
}
  • 实现UITableViewHeaderFooterView

1.2 实现UITableViewHeaderFooterView

案例:密码规则的说明 .h

#import <UIKit/UIKit.h>
#import "CRMPasswordRuleDescV.h"
NS_ASSUME_NONNULL_BEGIN
@interface CRMPasswordRuleDescHeaderFooterView : UITableViewHeaderFooterView
@property (nonatomic,weak) CRMPasswordRuleDescV *cellView;
@property (nonatomic, strong) CRMPasswordRuleDescModel* models;
@end

.m

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self selfInit];
//        [self createSubView];
//        [self addConstraints];
//        [self bindViewModel];
    }
    return self;
}
- (void)selfInit{
    self.backgroundColor = [UIColor whiteColor];
//    UITableViewHeaderFooterView
//    [self bgBottomView];
    [self cellView];
//    UITapGestureRecognizer *cutTap = [[UITapGestureRecognizer alloc] init];
//    [[cutTap rac_gestureSignal] subscribeNext:^(id x) {
//
//
//        NSLog(@" cutTap 点击了 ");
//        self.models.gotype = QCTreturnOrderModelblockType4jumpDetail;
//        if ( self.models.block) {
//            self.models.block(self.models);
//        }
//
//
//    }];
//    [self addGestureRecognizer:cutTap];
//
//
}
/**
 */
- (void)setModels:( id)models{
    _models =models;
    self.cellView.models = models;
}
- (CRMPasswordRuleDescV *)cellView{
    if (nil == _cellView) {
        CRMPasswordRuleDescV *tmpView = [[CRMPasswordRuleDescV alloc]init];
        _cellView = tmpView;
        _cellView.backgroundColor = rgb(255,255,255);
        [self.contentView addSubview:_cellView];
        __weak __typeof__(self) weakSelf = self;
        [_cellView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(weakSelf.contentView).offset(kAdjustRatio(0));
//            make.right.equalTo(weakSelf.contentView).offset(- kAdjustRatio(0));
            make.left.equalTo(weakSelf.contentView).offset(kAdjustRatio(0));
            make.right.equalTo(weakSelf.contentView).offset(- kAdjustRatio(0));
            make.top.equalTo(weakSelf.contentView).offset(kAdjustRatio(0));
            make.bottom.equalTo(weakSelf.contentView).offset(kAdjustRatio(0));
        }];
    }
    return _cellView;
}

CRMPasswordRuleDescV.h

#import "CRMPasswordRuleDescModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface CRMPasswordRuleDescV : UIView
@property (nonatomic, strong) CRMPasswordRuleDescModel* models;
@property (nonatomic,weak) UILabel *titleLab;
@end

CRMPasswordRuleDescV.m

- (instancetype)init {
    self = [super init];
    if (self) {////既然nil解析成NO,所以没有必要在条件语句比较。不要拿某样东西直接与YES比较,因为YES被定义为1
        // ...
        [self titleLab];
    }
    return self;
}
- (UILabel *)titleLab{
    if (nil == _titleLab) {
        UILabel *header = [[UILabel alloc]init];
        _titleLab = header;
        [self addSubview:_titleLab];
        header.textColor =  rgb(153,153,153);
        header.font = kPingFangNOkAdjustRatioFont(13);
        //    tmp.backgroundColor = ;
        header.numberOfLines = 0;
//        header.contentView.backgroundColor = self.tableView.backgroundColor;
        __weak __typeof__(self) weakSelf = self;
        [header mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf).offset(kAdjustRatio(13));
            make.left.equalTo(weakSelf).offset(kAdjustRatio(20));
            make.right.equalTo(weakSelf).offset(kAdjustRatio(-20));
            make.bottom.equalTo(weakSelf).offset(kAdjustRatio(-13));
        }];
    }
    return _titleLab;
}
- (void)setModels:(CRMPasswordRuleDescModel *)models{
    _models = models;
    self.titleLab.text = models.title;
}

1.3 其他案例

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case CRMRisk_merchant_editVSection4basicInfo:
        {
            return kAdjustRatio(30);
        }
            break;
        case CRMRisk_merchant_editVSection4UploadMaterials:
        {
            return kAdjustRatio(30);
        }
            break;
        default:
        {
            return kAdjustRatio(0);
        }
            break;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bacView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kAdjustRatio(30))];
    bacView.backgroundColor = rgb(248, 248, 248);
        bacView.height = KWratio(25);
        UILabel *titLab = [ControlManager text:@"基本信息" font:displayFontSize(12.0f) color:HWColor(153, 153, 153) alingment:0 fontWithName:@"PingFang-SC-Medium"];
        [bacView addSubview:titLab];
        [titLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(KWratio(15));
            make.centerY.offset(KWratio(0));
        }];
    switch (section) {
        case CRMRisk_merchant_editVSection4basicInfo:
        {
            titLab.text = @"基本信息";
        }
            break;
        case CRMRisk_merchant_editVSection4UploadMaterials:
        {
            titLab.text = @"风险处理";
        }
            break;
        default:
        {
            titLab.text = @"";
        }
            break;
    }
    return bacView;
//    UIView *tmp = [UIView new];
//
//    tmp.backgroundColor = self.backgroundColor;
//
//    return tmp;
}

II titleForHeaderInSection

//修改 tableViewSectionHeader 字体及背景色
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.textLabel.textColor =  rgb(153,153,153);
    header.textLabel.font = kPingFangFont(13);
    header.textLabel.numberOfLines = 0;
    header.contentView.backgroundColor = self.tableView.backgroundColor;
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (CRM_Change_PasswordViewSectionEnum4SurePassword ==  section) {
        return QCTLocal(@"新密码不少于6位,须包含大小写字母,数字和特殊字符");
    }
    return nil;
}

以上就是iOS开发自定义页脚和页眉技巧详解的详细内容,更多关于iOS自定义页脚页眉的资料请关注脚本之家其它相关文章!

相关文章

  • iOS设置圆角的三种方式

    iOS设置圆角的三种方式

    本文给大家分享ios设置圆角的三种方式,相对来说最简单的一种是第一种方法,具体内容详情参考下本文
    2017-03-03
  • Objective-C实现身份证验证的方法示例

    Objective-C实现身份证验证的方法示例

    这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • iOS WKWebview 白屏检测实现的示例

    iOS WKWebview 白屏检测实现的示例

    这篇文章主要介绍了iOS WKWebview 白屏检测实现的示例,帮助大家更好的进行ios开发,感兴趣的朋友可以了解下
    2020-10-10
  • iOS计算上次日期距离现在多久的代码

    iOS计算上次日期距离现在多久的代码

    这篇文章主要为大家详细介绍了iOS计算上次日期距离现在多久的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • iOS开发中实现新闻图片的无限循环展示的方法

    iOS开发中实现新闻图片的无限循环展示的方法

    这篇文章主要介绍了iOS开发中实现新闻图片的无限循环展示的方法,代码基于传统的Objective-C,需要的朋友可以参考下
    2015-12-12
  • IOSdrawRect实现雪花飘落效果

    IOSdrawRect实现雪花飘落效果

    这篇文章主要为大家详细介绍了IOSdrawRect实现雪花飘落效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • iOS 获取设备唯一标示符的方法详解

    iOS 获取设备唯一标示符的方法详解

    本篇文章主要介绍了iOS 获取设备唯一标示符的方法详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • iOS中正则表达式的运用示例代码

    iOS中正则表达式的运用示例代码

    正则表达式(广为所知的“regex”)是一个字符串或一个字符序列来说明一种模式,把它作为一个搜索字符串-非常强大!下面这篇文章主要给大家介绍了关于iOS中正则表达式运用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-09-09
  • iOS如何为圆角添加阴影效果示例代码

    iOS如何为圆角添加阴影效果示例代码

    最近一个项目中需要用到投影的效果,还要是圆角,通过查找相关的资料终于解决了,所以觉着有必要分享出来,下面这篇文章主要给大家介绍了关于iOS如何为圆角添加阴影效果的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10
  • Objective-C 入门篇(推荐)

    Objective-C 入门篇(推荐)

    由C语言和Smalltalk扩展出来的,是C语言的超集,最大的区别是OC是面向对象的,其火星文写法对于之前从事Java开发的同学颇感蛋疼,OC最大特点之一是使用消息结构而不是函数调用
    2021-11-11

最新评论