iOS 修饰符~NS_DESIGNATED_INITIALIZER

部署运行你感兴趣的模型镜像

//联系人:石虎  QQ: 1224614774 昵称:嗡嘛呢叭咪哄

一、NS_DESIGNATED_INITIALIZER

     

  用来修饰init方法,被修饰的方法称为designated initializer;没有被这个修饰的init方法称为convenience initializer

 

1.designated initializer 必须调用父类的designated initializer (最终指向也会有警告)

2.convenience initializer最终必须指向一个designated initializer

3.designated initializer的类必须实现父类的designated initializer方法 

 

二、补充点


  1.继承性,当子类里面没有其他的init方法时,继承父类的designated initializer;如果子类有其他的init方法,则父类的 designated initializer方法 都变成了  convenience initializer

 

  2.NSCoding initWithCoder:是一个 designated initializer方法


谢谢!!!


 

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

你贴出的 `.h` 文件内容非常关键,我们现在来 **逐行解读这个 `TPBCommonTableController` 到底是什么、能干什么、以及你在继承时该怎么用它**。 --- ## 🔍 一、从头文件看本质:这是什么? ```objc @interface TPBCommonTableController : TPBBaseViewController <UITableViewDataSource, UITableViewDelegate, TPBTableSearchHeaderDelegate> ``` ### ✅ 关键信息1:它不是 UITableViewController! - 它继承的是 `TPBBaseViewController`(而不是 `UITableViewController`) - 但它自己持有一个 `tableView` 属性,并实现了 `UITableViewDataSource` 和 `delegate` - 这意味着:它是“手动集成 tableView”的控制器,更灵活,适合复杂布局 > 📌 类比:不是原装车,是改装车底盘 + 自己装发动机 --- ### ✅ 关键信息2:它已经帮你实现了表格的核心逻辑 因为它遵守了: - `UITableViewDataSource` → 数据源 - `UITableViewDelegate` → 交互控制 - `TPBTableSearchHeaderDelegate` → 支持搜索栏功能 👉 所以你不需要再写 `numberOfRowsInSection` 或 `cellForRowAt`! --- ## 🧩 二、重要属性详解(你要怎么用) | 属性 | 作用 | 使用方式 | |------|------|---------| | `@property (nonatomic, readonly, strong) TPKeyboardAvoidingTableView *tableView;` | 内置一个防键盘遮挡的 tableView | 只读,不能替换,但可以配置样式 | | `@property (nonatomic, strong) NSArray<TPBTableSectionModel *> *sectionArray;` | 【核心】驱动整个列表显示的数据源 | ✅ 你必须设置这个数组! | | `@property (nonatomic, assign) BOOL searchBarShow;` | 是否显示顶部搜索栏 | 设置为 `YES` 即可自动出现 | | `@property (nonatomic, copy) NSString *searchPlaceholder;` | 搜索框占位文字 | 如 `"请输入关键词"` | | `@property (nonatomic, nullable, strong) UIView *customTableHeaderView;` | 自定义 table 头部视图 | 可放 banner、用户信息等 | | `@property (nonatomic, assign) BOOL hideCellSeparator;` | 是否隐藏 cell 分割线 | 默认有线,设为 YES 隐藏 | 📌 特别注意:`tableView` 是只读的!你不该去重新赋值它。 --- ## 🛠️ 三、初始化方法说明 ```objc - (instancetype)init; - (instancetype)initWithTableViewStyle:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER; - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; ``` ### ✅ 你应该使用哪个构造器? #### 场景1:普通页面(推荐) ```swift MyListViewController *vc = [[MyListViewController alloc] init]; // 或指定样式 MyListViewController *vc = [[MyListViewController alloc] initWithTableViewStyle:UITableViewStylePlain]; ``` #### 场景2:如果要用 xib/storyboard 那就必须实现 `initWithNibName:bundle:` > ⚠️ 注意:如果你没写 xib,就不要调这个 --- ## 🎯 四、两个关键方法的作用 ```objc - (void)updateSearchKey:(nullable NSString *)searchKey; - (void)focusToFirstEligibleElement; ``` ### 1. `updateSearchKey:` - 当你在搜索框输入时,会触发这个方法 - 子类可以重写它来做过滤逻辑 ```objc // 示例:在你的子类中 - (void)updateSearchKey:(NSString *)searchKey { [super updateSearchKey:searchKey]; // 先让父类处理 // 然后你自己做筛选 NSArray *filteredSections = [self filterSectionsWithKeyword:searchKey]; self.sectionArray = filteredSections; [self.tableView reloadData]; } ``` ### 2. `focusToFirstEligibleElement` - 让第一个可交互元素(如输入框)获得焦点 - 常用于表单页自动弹起键盘 ```objc [self focusToFirstEligibleElement]; // 调用即可 ``` --- ## 🧪 五、你现在新建页面的标准模板代码 假设你要做一个「个人资料设置页」 ### Step 1:创建控制器并继承 ```swift // ProfileSettingViewController.h @interface ProfileSettingViewController : TPBCommonTableController @end ``` ```swift // ProfileSettingViewController.m #import "ProfileSettingViewController.h" @implementation ProfileSettingViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"个人信息"; // 显示搜索框? self.searchBarShow = NO; // 设置数据 [self setupData]; } - (void)setupData { // 创建 Section TPBTableSectionModel *section = [[TPBTableSectionModel alloc] init]; // Cell 1: 昵称输入框 TPBCommonInputTableCellModel *nameCell = [[TPBCommonInputTableCellModel alloc] init]; nameCell.title = @"昵称"; nameCell.text = @"张三"; nameCell.placeholder = @"请输入昵称"; nameCell.textFieldDidChangeCallback = ^(NSString *text) { NSLog(@"昵称变化: %@", text); }; // Cell 2: 性别选择 TPBJumpSelectTableCellModel *genderCell = [[TPBJumpSelectTableCellModel alloc] init]; genderCell.title = @"性别"; genderCell.subtitle = @"男"; genderCell.didSelectCellCallback = ^(NSIndexPath *indexPath, TPBBaseTableCellModel *model) { // 弹出选择器 [self showGenderPickerWithCompletion:^(NSString *selectedGender) { genderCell.subtitle = selectedGender; [self.tableView reloadData]; }]; }; // 组装数据 section.cellModelArray = @[nameCell, genderCell]; self.sectionArray = @[section]; // 刷新 UI [self.tableView reloadData]; } @end ``` ✅ 效果: - 自动渲染两个 cell - 输入框支持打字监听 - 点击“性别”弹出选择器并更新副标题 --- ## ❗ 六、你最容易犯的错误(避坑指南) | 错误 | 正确做法 | |------|--------| | 重写 `numberOfSectionsInTableView:` | ❌ 不要写!会被父类覆盖 | | 手动设置 `dataSource = self` | ❌ 不要写!父类已经设置了 | | 忘记调 `reloadData()` | ❌ 改完 `sectionArray` 后必须刷新 | | 直接访问 tableView 的 subviews | ❌ 封装层级深,容易崩溃 | | 修改 `sectionArray` 后不 reload | ❌ 数据变了但界面没变 | --- ## ✅ 总结:你现在只需要记住这几件事 > 🎯 **你作为使用者的目标是:把数据准备好,剩下的交给框架** ### ✅ 你需要做的: 1. **继承 `TPBCommonTableController`** 2. **构造 `sectionArray` 数组(用各种 cell model)** 3. **给每个 cell model 设置点击/输入回调** 4. **调用 `[self.tableView reloadData]` 刷新界面** 5. (可选)开启搜索栏并实现 `updateSearchKey:` 做过滤 ### ❌ 你绝对不要做的: - 不要写任何 `UITableViewDataSource` 方法 - 不要改 `tableView.dataSource` - 不要手动添加 cell 到 tableView ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值