iOS開發(fā)自定義頁腳和頁眉技巧詳解
前言
應(yīng)用場景:
- 商品管理列表的頁眉顯示商品數(shù)量和批量操作
- 修改界面對密碼規(guī)則的說明
I 自定義頁腳和頁眉
如果系統(tǒng)的APItitleForHeaderInSection
滿足不了你的需求,可以自定義UITableViewHeaderFooterView。
1.1 自定義分組頁眉的步驟
自定義UITableViewHeaderFooterView的步驟:
- 注冊
[_tableView registerClass:[CRMPasswordRuleDescHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"CRMPasswordRuleDescHeaderFooterView"];
- 創(chuàng)建
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { CRMPasswordRuleDescHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CRMPasswordRuleDescHeaderFooterView"]; // footerView.type = self.type; footerView.models = self.viewModel.passwordRuleDescModel; return footerView; }
- 實現(xiàn)UITableViewHeaderFooterView
1.2 實現(xiàn)UITableViewHeaderFooterView
案例:密碼規(guī)則的說明 .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,所以沒有必要在條件語句比較。不要拿某樣?xùn)|西直接與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 = @"風(fēng)險處理"; } 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位,須包含大小寫字母,數(shù)字和特殊字符"); } return nil; }
以上就是iOS開發(fā)自定義頁腳和頁眉技巧詳解的詳細內(nèi)容,更多關(guān)于iOS自定義頁腳頁眉的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IOS中MMDrawerController第三方抽屜效果的基本使用示例
這篇文章主要介紹了IOS中MMDrawerController第三方抽屜效果的基本使用示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02ios利用RunLoop原理實現(xiàn)去監(jiān)控卡頓實例詳解
這篇文章主要為大家介紹了ios利用RunLoop原理實現(xiàn)去監(jiān)控卡頓實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09iOS開發(fā)學(xué)習(xí)之監(jiān)測程序的崩潰次數(shù)詳解
iOS開發(fā)中遇到程序崩潰是很正常的事情,下面這篇文章主要給大家介紹了關(guān)于iOS如何監(jiān)測程序崩潰次數(shù)的相關(guān)資料,文中通過詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07iOS實現(xiàn)調(diào)用QQ客戶端發(fā)起臨時會話
本篇文章主要給大家詳細分析了用IOS實現(xiàn)調(diào)用QQ客戶端發(fā)起臨時會話的功能,對此有需要的朋友收藏分享下。2018-02-02IOS 開發(fā)之應(yīng)用喚起實現(xiàn)原理詳解
這篇文章主要介紹了IOS 開發(fā)之應(yīng)用喚起實現(xiàn)原理詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12