iOS开发基础知识回顾(一)

很多时候写代码只记得怎么去写实现功能,没有去稍微理解为什么,通过一些讨论,记录下以前理解错误的地方。

1.Tableview复用问题

在此之前,都使用cell != nil的方式来判断是否需要重新创建cell,后来新的API出来之后新的方法出来了。

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(6.0)); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

文档上很清晰的说明了,使用dequeueReusableCellWithIdentifier:forIndexPath需要配合register,此种情况不需要判断cell != nil,否则会出现cell复用为空导致崩溃的情况,或者重复创建影响性能。

代码测试发现使用注册+dequeueReusableCellWithIdentifier 使用也没有出现崩溃问题或者卡顿,数据定位10000条,有懂这个的同学可以帮忙指点下。虽然这种写法大家都不推荐,但是丝毫没感觉到有问题。。。

2.NavigationBar背景色的问题,UINavigationBarAppearance的使用

在iOS中,通过使用UINavigationBarAppearance类,你可以轻松地自定义导航栏的外观属性。UINavigationBarAppearance类提供了一种在整个应用程序中一致应用导航栏样式的方法。

你可以按照以下步骤来使用UINavigationBarAppearance:

创建一个UINavigationBarAppearance对象:
let appearance = UINavigationBarAppearance()

根据你的需求,设置导航栏的背景色、渐变色、阴影等属性:
swift
Copy
// 设置背景色
appearance.backgroundColor = UIColor.red

// 设置渐变色
let gradientColors = [UIColor.red, UIColor.blue]
appearance.backgroundGradientColors = gradientColors

// 设置阴影
appearance.shadowColor = UIColor.gray
应用设置的外观属性到导航栏:

navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
你可以通过standardAppearance属性设置导航栏在非滚动状态下的外观,而通过scrollEdgeAppearance属性设置导航栏在滚动到边缘位置时的外观。

使用UINavigationBarAppearance,你可以定义导航栏的多个外观样式,并根据需要在不同的视图控制器中进行切换。这样,你可以实现全局的导航栏样式一致性。

请注意,UINavigationBarAppearance类是在iOS 13及更高版本引入的,如果你的目标版本较低,可能无法使用该类。
要在iOS中使用UINavigationBarAppearance类将导航栏设置为透明,你可以按照以下步骤进行操作:

创建一个UINavigationBarAppearance对象:

let appearance = UINavigationBarAppearance()
设置导航栏背景透明度为0:

appearance.backgroundColor = UIColor.clear
appearance.backgroundEffect = UIBlurEffect(style: .regular) // 可选,添加模糊效果
应用设置的外观属性到导航栏:

navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
这将把导航栏的背景色设置为透明。你还可以选择添加模糊效果(使用UIBlurEffect)以增强视觉效果。

请注意,上述代码是在iOS 13及更高版本中使用UINavigationBarAppearance类的示例。如果你的目标版本较低,你可以考虑使用其他方法,如在视图控制器中设置导航栏透明(可以参考之前提供的方法)。

3.model中添加block

在 iOS 模型(Model)中添加 Block(块)可以通过以下步骤进行:

定义 Block 类型:在模型的头文件中(通常以.h为后缀),使用typedef关键字定义一个 Block 类型。例如,以下是定义一个无返回值且带有一个字符串参数的 Block 类型的示例:

typedef void (^MyBlock)(NSString *text);
添加 Block 属性:在模型的接口部分(通常是头文件中的@interface声明)中,添加一个属性来保存 Block。例如:

@property (nonatomic, copy) MyBlock myBlock;
使用 Block:在模型的实现部分(通常是以.m为后缀的实现文件)中,可以通过调用 Block 来执行特定的操作。例如:

- (void)performBlock {
    if (self.myBlock) {
        self.myBlock(@"Hello, Block!");
    }
}
在上述示例中,performBlock方法检查 myBlock 属性是否存在,并将字符串参数传递给 Block。

外部使用 Block:在使用模型的其他类中,可以设置模型的 Block 属性并实现 Block 的具体操作。例如:

MyModel *model = [[MyModel alloc] init];
model.myBlock = ^(NSString *text) {
    NSLog(@"%@", text);
};

[model performBlock]; // 执行模型中的 Block
在上述示例中,我们创建了一个模型对象model,设置了myBlock属性的实现,并在调用performBlock方法时执行了 Block。

通过上述步骤,你可以在 iOS 模型中添加和使用 Block,以实现特定的功能和回调操作。请根据你的具体需求和模型的设计进行适当的调整和修改。

4.Masony不常用的方法 多个视图一起约束

- (UIScrollView *)baseScrollView{
    if (!_baseScrollView) {
        _baseScrollView = [[UIScrollView alloc] init];
        _baseScrollView.backgroundColor = COLOR_WITH_HEX(0xf5f6f7);
        UIView *nameView = [self nameView];
        [_baseScrollView addSubview:nameView];
        [nameView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(10);
            make.height.mas_equalTo(290);
        }];
        
        [_baseScrollView addSubview:self.photoView];
        [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(nameView.mas_bottom).offset(10);
            make.height.mas_equalTo(145);
        }];
        
        UIView *tagsView = [self tagsView];
        [_baseScrollView addSubview:tagsView];
        [tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.photoView.mas_bottom).offset(10);
            make.height.mas_equalTo(110);
        }];
        
        [@[nameView, self.photoView, tagsView] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_baseScrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-24);
        }];
    }
    return _baseScrollView;
}
NEPHealthAddReportViewController

参考

5.App内版本升级提示

applicationWillEnterForeground方法在App启动过程中是不会执行的,所以App内的版本升级提示一般写在didFinishLaunchingWithOptions方法里面

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容