目录前言I 自定义页脚和页眉1.1 自定义分组页眉的步骤1.2 实现UITableViewHeaderFooterView1.3 其他案例II titleForHeaderInSec
应用场景:
如果系统的apititleForHeaderInSection
满足不了你的需求,可以自定义UITableViewHeaderFooterView。
自定义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;
}
案例:密码规则的说明 .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;
}
- (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;
}
//修改 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;
}
--结束END--
本文标题: iOS开发自定义页脚和页眉技巧详解
本文链接: https://lsjlt.com/news/165257.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0