iOS - (instancetype)initWithCoder:(NSCoder *)aDecoder采坑小记
发布日期:2022-04-05 00:52:12
浏览次数:1
分类:博客文章
本文共 2331 字,大约阅读时间需要 7 分钟。
一般我们封装控件时 既要支持xib 又要支持手码 一般我们会在以下两个方法里执行我们的自定义操作。然后关于initWithCoder的小坑来了。
/// 手码- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 自定义操作 [self initData]; [self initUI]; } return self;}/// xib- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { // 自定义操作 [self initData]; [self initUI]; } return self;}
我是在view上包装了一个 UITableView
- (void)initUI { self.backgroundColor = [UIColor clearColor]; [self addSubview:self.appendixTableView]; [self.appendixTableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.mas_top); make.left.equalTo(self.mas_left); make.bottom.equalTo(self.mas_bottom); make.right.equalTo(self.mas_right); }]; [self.appendixTableView registerClass:[iComeAppendixTableViewCell class] forCellReuseIdentifier:iComeAppendixTableViewCellReused]; }- (UITableView *)appendixTableView { if (!_appendixTableView) { _appendixTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _appendixTableView.delegate = self; _appendixTableView.dataSource = self; _appendixTableView.backgroundColor = [UIColor clearColor]; _appendixTableView.separatorStyle = UITableViewCellSeparatorStyleNone; _appendixTableView.bounces = NO; _appendixTableView.showsHorizontalScrollIndicator = NO; _appendixTableView.showsVerticalScrollIndicator = NO; if (@available(iOS 11.0, *)) { _appendixTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else{ self.viewController.automaticallyAdjustsScrollViewInsets = NO; } } return _appendixTableView;}
然后发现设置的tableview的某些属性不起作用,比如背景色、分割线、等。各种方法试验过后发现问题在这里
// 包装UITableView会有问题 包装UICollectionView不会有问题//- (instancetype)initWithCoder:(NSCoder *)aDecoder {// self = [super initWithCoder:aDecoder];// if (self) {// [self initData];// [self initUI];// }// return self;//}// 在awakeFromNib方法中不会有问题- (void)awakeFromNib { [super awakeFromNib]; [self initData]; [self initUI];}
综上所述:
initWithCoder 包装UITableView会有问题 所以最好都在awakeFromNib中添加自定义操作
转载地址:https://www.cnblogs.com/lijianyi/p/11549875.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月16日 09时45分53秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux Shell编程变量赋值和引用
2019-04-30
计算机专业推荐书籍
2019-04-30
程序员的成长之路
2019-04-30
linux下CPU温度监测
2019-04-30
java数组查找元素索引,无需排序
2019-04-30
QT 多线程程序设计
2019-04-30
Qt 4.8.4 & Qt Creator 2.6.1 安装和配置(Windows)
2019-04-30
Qt使用自定义类型信号槽注册问题
2019-04-30
mysql远程数据库打开很慢的问题
2019-04-30
Anroid Studio工具UI设计默认切换焦点顺序原则
2019-04-30
Qt数据库操作 连接SQLite和MySQL数据库实例
2019-04-30
Qt关于mysql数据库断开重连的问题
2019-04-30
修改root登录用户名减少阿里云Linux主机“被暴力破解”警告
2019-04-30
linux下RPM工具的使用
2019-04-30
dynamic_cast
2019-04-30
如何将QT应用程序打包发布
2019-04-30
Qt在Windows数据库的连接
2019-04-30
Qt远程连接MySql数据库
2019-04-30
Windows下Qt的MYSQL数据库开发步骤
2019-04-30
QtCreator中常用快捷键总结
2019-04-30