開發中遇到的坑

1. tableView使用時發現的問題

1.1 tableView 加載cell 崩潰:_configureCellForDisplay:forIndexPath

1.1.  Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:8042

分析: 控制器未聲明實現協議tableviewDelegate  ,tableviewDataSourse  ; 錯誤發生在注冊cell 的使用  

ps: 另外還有常見的xib 中的cell 未設置相應的identifiter  ;  還會遇到未設置代理:數據源,導致協議的方法不執行的問題


1.2 tableview 的UI細節

a. 分割線是否顯示:self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
b. 點擊后恢復點擊狀態: tableView.deselectRow(at: indexPath, animated: true)

1.3 tableView 加載問題

描述:嵌套使用collectionView 和tableView ,在collectionCell 的layoutSubviews 里面注冊 tableView的cell,導致崩潰,原因是注冊時機太晚,加載tableCell 的時候,發現tableCell 沒有注冊成功,重用失敗崩潰
在ios8.3/8.4  必崩

總計:tableCell 的注冊應該放在collectionCell 的awakeFromNib 方法里面,在一開始加載xib 的的時候就注冊就可以解決問題了,這是對*awakeFromNib* 和**layoutSubviews** 的加載時機沒有理解正確導致的

正確例子如下:awakeFromNib 是collectionCell 的方法:
- (void)awakeFromNib{
    [super awakeFromNib];
    [self.tableView registerClass:[UITableViewCellVedio class] forCellReuseIdentifier:[UITableViewCellVedio identifer_cell]];
}

錯誤例子如下:
/*崩潰在ios8.3/8.4 上面;改成上面的額方式注冊*/
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.tableView registerClass:[HomeWorkQuestionNameCell class] forCellReuseIdentifier:QuestionNameCell];
    [self.tableView registerClass:[UITableViewCellVedio class] forCellReuseIdentifier:[UITableViewCellVedio identifer_cell]];    
}

1.4 tableView 加載問題(ios8)

描述:崩潰提示*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:6245

執行cellforRow cell 生成崩潰,只發生在ios 8的系統上面
錯誤代碼:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerNib:[UINib nibWithNibName:@"LearningCenterServiceCell" bundle:nil] forCellReuseIdentifier:@"LearningCenterServiceCell"];
}
錯誤分析:setSeparatorStyle方法   導致viewLoad 里面的代碼加載順序發生變化,沒有執行注冊cell 的方法就執行了tableView 的代理方法,重用方式生成cell 的時候,cell 沒有注冊導致方法崩潰

正確代碼:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    [self.tableView registerNib:[UINib nibWithNibName:@"LearningCenterServiceCell" bundle:nil] forCellReuseIdentifier:@"LearningCenterServiceCell"];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
總結:盡可能早的讓cell 注冊是防止iOS SDK 差異導致代碼執行順序和一些方法的時機出現問題導致crash ,這里面的坑之前比較少遇到因為很多時候不手動代碼設置separatorStyle ,在xib 里面設置比較多,所以沒有出現過問題,今天就猜到了,不過>=9.0 的系統蘋果已經調整這個限制,順序沒那么關鍵了

1.5 tableView 的reloadSections 刷新方法崩潰:
具體分析參考:iOS的那些bug崩潰 中的第二條

1.6 關于tabelView 的方法細節:cellForRowAtIndexPath 和willDisplayCell 的差異

cellForRowAtIndexPath方法只負責創建cell,cellForRowAtIndexPath是data source協議中一個必須實現的方法,willDisplayCell方法才給cell進行賦值操作,willDisplayCell是delegate協議中一個可選的方法。
tableView: cellForRowAtIndexPath: 創建或者從重用隊列里面取出cell,不會馬上顯示,所以不要在這里對cell進行數據填充
tableView: willDisplayCell: forRowAtIndexPath: 這里就是需要顯示了,所以數據填充在這里進行,
以上只是針對比較復雜的cell才這樣做,一般的都放在tableView: cellForRowAtIndexPath: 即可

2.swift 調用oc 的時候,方法不能自動匹配識別,需要手動拼寫轉換后的方法<首先要保證oc頭文件引入到橋接文件中>

oc 方法:
+ (void)produceNavSeatWorkWithCourseId:(NSInteger)cid lessonId:(NSInteger)lid   type:(HomeWorkType)type    success:(void(^)(SeatWorkViewController * seatWork,BOOL isDone))success failure:(void(^)(NSError * error))failure;

轉換后的swift 方法:
  SeatWorkViewControllerManager.produceNavSeatWork(withCourseId:cid, lessonId:lid,type:HomeWorkType.nomal, success: { (seatWork:SeatWorkViewController!,isDone:Bool) in
         //doSomthing

   }) {(error:Error?) in
            

     }

3. 0 xib設置UITextField的私有屬性placeholderColor

在使用UITextField時發現在公開的屬性里面并沒有用來設置placeholderColor的屬性,這樣很難滿足設計師的需求,通過xib可以輕松設置UITextField的placeholderColor,同樣首先也需要進入上面步驟的那個頁面,然后添加 placeholderLabel.textColor 的鍵值對來設置顏色


這里寫圖片描述
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容