iOS開發(fā)基礎(chǔ)知識回顧(一)

很多時候?qū)懘a只記得怎么去寫實現(xiàn)功能,沒有去稍微理解為什么,通過一些討論,記錄下以前理解錯誤的地方。

1.Tableview復(fù)用問題

在此之前,都使用cell != nil的方式來判斷是否需要重新創(chuàng)建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,否則會出現(xiàn)cell復(fù)用為空導(dǎo)致崩潰的情況,或者重復(fù)創(chuàng)建影響性能。

代碼測試發(fā)現(xiàn)使用注冊+dequeueReusableCellWithIdentifier 使用也沒有出現(xiàn)崩潰問題或者卡頓,數(shù)據(jù)定位10000條,有懂這個的同學(xué)可以幫忙指點下。雖然這種寫法大家都不推薦,但是絲毫沒感覺到有問題。。。

2.NavigationBar背景色的問題,UINavigationBarAppearance的使用

在iOS中,通過使用UINavigationBarAppearance類,你可以輕松地自定義導(dǎo)航欄的外觀屬性。UINavigationBarAppearance類提供了一種在整個應(yīng)用程序中一致應(yīng)用導(dǎo)航欄樣式的方法。

你可以按照以下步驟來使用UINavigationBarAppearance:

創(chuàng)建一個UINavigationBarAppearance對象:
let appearance = UINavigationBarAppearance()

根據(jù)你的需求,設(shè)置導(dǎo)航欄的背景色、漸變色、陰影等屬性:
swift
Copy
// 設(shè)置背景色
appearance.backgroundColor = UIColor.red

// 設(shè)置漸變色
let gradientColors = [UIColor.red, UIColor.blue]
appearance.backgroundGradientColors = gradientColors

// 設(shè)置陰影
appearance.shadowColor = UIColor.gray
應(yīng)用設(shè)置的外觀屬性到導(dǎo)航欄:

navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
你可以通過standardAppearance屬性設(shè)置導(dǎo)航欄在非滾動狀態(tài)下的外觀,而通過scrollEdgeAppearance屬性設(shè)置導(dǎo)航欄在滾動到邊緣位置時的外觀。

使用UINavigationBarAppearance,你可以定義導(dǎo)航欄的多個外觀樣式,并根據(jù)需要在不同的視圖控制器中進(jìn)行切換。這樣,你可以實現(xiàn)全局的導(dǎo)航欄樣式一致性。

請注意,UINavigationBarAppearance類是在iOS 13及更高版本引入的,如果你的目標(biāo)版本較低,可能無法使用該類。
要在iOS中使用UINavigationBarAppearance類將導(dǎo)航欄設(shè)置為透明,你可以按照以下步驟進(jìn)行操作:

創(chuàng)建一個UINavigationBarAppearance對象:

let appearance = UINavigationBarAppearance()
設(shè)置導(dǎo)航欄背景透明度為0:

appearance.backgroundColor = UIColor.clear
appearance.backgroundEffect = UIBlurEffect(style: .regular) // 可選,添加模糊效果
應(yīng)用設(shè)置的外觀屬性到導(dǎo)航欄:

navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
這將把導(dǎo)航欄的背景色設(shè)置為透明。你還可以選擇添加模糊效果(使用UIBlurEffect)以增強視覺效果。

請注意,上述代碼是在iOS 13及更高版本中使用UINavigationBarAppearance類的示例。如果你的目標(biāo)版本較低,你可以考慮使用其他方法,如在視圖控制器中設(shè)置導(dǎo)航欄透明(可以參考之前提供的方法)。

3.model中添加block

在 iOS 模型(Model)中添加 Block(塊)可以通過以下步驟進(jìn)行:

定義 Block 類型:在模型的頭文件中(通常以.h為后綴),使用typedef關(guān)鍵字定義一個 Block 類型。例如,以下是定義一個無返回值且?guī)в幸粋€字符串參數(shù)的 Block 類型的示例:

typedef void (^MyBlock)(NSString *text);
添加 Block 屬性:在模型的接口部分(通常是頭文件中的@interface聲明)中,添加一個屬性來保存 Block。例如:

@property (nonatomic, copy) MyBlock myBlock;
使用 Block:在模型的實現(xiàn)部分(通常是以.m為后綴的實現(xiàn)文件)中,可以通過調(diào)用 Block 來執(zhí)行特定的操作。例如:

- (void)performBlock {
    if (self.myBlock) {
        self.myBlock(@"Hello, Block!");
    }
}
在上述示例中,performBlock方法檢查 myBlock 屬性是否存在,并將字符串參數(shù)傳遞給 Block。

外部使用 Block:在使用模型的其他類中,可以設(shè)置模型的 Block 屬性并實現(xiàn) Block 的具體操作。例如:

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

[model performBlock]; // 執(zhí)行模型中的 Block
在上述示例中,我們創(chuàng)建了一個模型對象model,設(shè)置了myBlock屬性的實現(xiàn),并在調(diào)用performBlock方法時執(zhí)行了 Block。

通過上述步驟,你可以在 iOS 模型中添加和使用 Block,以實現(xiàn)特定的功能和回調(diào)操作。請根據(jù)你的具體需求和模型的設(shè)計進(jìn)行適當(dāng)?shù)恼{(diào)整和修改。

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內(nèi)版本升級提示

applicationWillEnterForeground方法在App啟動過程中是不會執(zhí)行的,所以App內(nèi)的版本升級提示一般寫在didFinishLaunchingWithOptions方法里面

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容