1. tableView使用時(shí)發(fā)現(xiàn)的問題
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
分析: 控制器未聲明實(shí)現(xiàn)協(xié)議tableviewDelegate ,tableviewDataSourse ; 錯(cuò)誤發(fā)生在注冊(cè)cell 的使用
ps: 另外還有常見的xib 中的cell 未設(shè)置相應(yīng)的identifiter ; 還會(huì)遇到未設(shè)置代理:數(shù)據(jù)源,導(dǎo)致協(xié)議的方法不執(zhí)行的問題
1.2 tableview 的UI細(xì)節(jié)
a. 分割線是否顯示:self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
b. 點(diǎn)擊后恢復(fù)點(diǎn)擊狀態(tài): tableView.deselectRow(at: indexPath, animated: true)
1.3 tableView 加載問題
描述:嵌套使用collectionView 和tableView ,在collectionCell 的layoutSubviews 里面注冊(cè) tableView的cell,導(dǎo)致崩潰,原因是注冊(cè)時(shí)機(jī)太晚,加載tableCell 的時(shí)候,發(fā)現(xiàn)tableCell 沒有注冊(cè)成功,重用失敗崩潰
在ios8.3/8.4 必崩
總計(jì):tableCell 的注冊(cè)應(yīng)該放在collectionCell 的awakeFromNib 方法里面,在一開始加載xib 的的時(shí)候就注冊(cè)就可以解決問題了,這是對(duì)*awakeFromNib* 和**layoutSubviews** 的加載時(shí)機(jī)沒有理解正確導(dǎo)致的
正確例子如下:awakeFromNib 是collectionCell 的方法:
- (void)awakeFromNib{
[super awakeFromNib];
[self.tableView registerClass:[UITableViewCellVedio class] forCellReuseIdentifier:[UITableViewCellVedio identifer_cell]];
}
錯(cuò)誤例子如下:
/*崩潰在ios8.3/8.4 上面;改成上面的額方式注冊(cè)*/
-(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
執(zhí)行cellforRow cell 生成崩潰,只發(fā)生在ios 8的系統(tǒng)上面
錯(cuò)誤代碼:
- (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"];
}
錯(cuò)誤分析:setSeparatorStyle方法 導(dǎo)致viewLoad 里面的代碼加載順序發(fā)生變化,沒有執(zhí)行注冊(cè)cell 的方法就執(zhí)行了tableView 的代理方法,重用方式生成cell 的時(shí)候,cell 沒有注冊(cè)導(dǎo)致方法崩潰
正確代碼:
- (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];
}
總結(jié):盡可能早的讓cell 注冊(cè)是防止iOS SDK 差異導(dǎo)致代碼執(zhí)行順序和一些方法的時(shí)機(jī)出現(xiàn)問題導(dǎo)致crash ,這里面的坑之前比較少遇到因?yàn)楹芏鄷r(shí)候不手動(dòng)代碼設(shè)置separatorStyle ,在xib 里面設(shè)置比較多,所以沒有出現(xiàn)過問題,今天就猜到了,不過>=9.0 的系統(tǒng)蘋果已經(jīng)調(diào)整這個(gè)限制,順序沒那么關(guān)鍵了
1.5 tableView 的reloadSections 刷新方法崩潰:
具體分析參考:iOS的那些bug崩潰 中的第二條
1.6 關(guān)于tabelView 的方法細(xì)節(jié):cellForRowAtIndexPath 和willDisplayCell 的差異
cellForRowAtIndexPath方法只負(fù)責(zé)創(chuàng)建cell,cellForRowAtIndexPath是data source協(xié)議中一個(gè)必須實(shí)現(xiàn)的方法,willDisplayCell方法才給cell進(jìn)行賦值操作,willDisplayCell是delegate協(xié)議中一個(gè)可選的方法。
tableView: cellForRowAtIndexPath: 創(chuàng)建或者從重用隊(duì)列里面取出cell,不會(huì)馬上顯示,所以不要在這里對(duì)cell進(jìn)行數(shù)據(jù)填充
tableView: willDisplayCell: forRowAtIndexPath: 這里就是需要顯示了,所以數(shù)據(jù)填充在這里進(jìn)行,
以上只是針對(duì)比較復(fù)雜的cell才這樣做,一般的都放在tableView: cellForRowAtIndexPath: 即可
2.swift 調(diào)用oc 的時(shí)候,方法不能自動(dòng)匹配識(shí)別,需要手動(dòng)拼寫轉(zhuǎn)換后的方法<首先要保證oc頭文件引入到橋接文件中>
oc 方法:
+ (void)produceNavSeatWorkWithCourseId:(NSInteger)cid lessonId:(NSInteger)lid type:(HomeWorkType)type success:(void(^)(SeatWorkViewController * seatWork,BOOL isDone))success failure:(void(^)(NSError * error))failure;
轉(zhuǎn)換后的swift 方法:
SeatWorkViewControllerManager.produceNavSeatWork(withCourseId:cid, lessonId:lid,type:HomeWorkType.nomal, success: { (seatWork:SeatWorkViewController!,isDone:Bool) in
//doSomthing
}) {(error:Error?) in
}
3. 0 xib設(shè)置UITextField的私有屬性placeholderColor
在使用UITextField時(shí)發(fā)現(xiàn)在公開的屬性里面并沒有用來設(shè)置placeholderColor的屬性,這樣很難滿足設(shè)計(jì)師的需求,通過xib可以輕松設(shè)置UITextField的placeholderColor,同樣首先也需要進(jìn)入上面步驟的那個(gè)頁面,然后添加 placeholderLabel.textColor 的鍵值對(duì)來設(shè)置顏色
這里寫圖片描述