tableView是經(jīng)常使用的控件之一,有了tableView就離不開cell。一般cell的使用都是復用使用的,
cell的加載經(jīng)常使用的有兩種方法,一種就是直接如下方法去獲取cell
#pragma mark- UITableViewDataSource
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*identifier =@"MyTableViewCell";
MyTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier ];
if(!cell) {
cell = [[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
}
returncell;
}
另外一種是通過registerClass: forCellReuseIdentifier:把tableView和cell聯(lián)系在一塊
- (UITableView*)myTableView
{
if(!_myTableView) {
UITableView* myTableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
myTableView.delegate=self;
myTableView.dataSource=self;
//注冊cell
[myTableView registerClass:[MyTableViewCellclass] forCellReuseIdentifier:@"MyTableViewCell"];
//或者這么注冊
//[myTableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyTableViewCell"];
_myTableView= myTableView;
}
return_myTableView;
}
#pragma mark- UITableViewDataSource
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
MyTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"MyTableViewCell"forIndexPath:indexPath];
returncell;
}
最近遇見一個奇葩的事情,可能有點繞,一點點來吧。說一下這個第二種加載cell的方式,
一般在注冊cell的時候<registerClass: forCellReuseIdentifier:或者registerNib: forCellReuseIdentifier:>的同時會去設置下tableView的相關(guān)的屬性,比如設置一下分割線的樣式之類的。
代碼如下
- (UITableView*)myTableView
{
if(!_myTableView) {
UITableView* myTableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
myTableView.delegate=self;
myTableView.dataSource=self;
myTableView.tableFooterView= [UIViewnew];
myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
[myTableViewregisterClass:[MyTableViewCellclass]forCellReuseIdentifier:@"MyTableViewCell"];
_myTableView= myTableView;
}
return_myTableView;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
MyTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"MyTableViewCell"forIndexPath:indexPath];
returncell;
}
iOS8.4真機,一運行,發(fā)現(xiàn)崩潰了。ps:使用了8.0,8.4,9.0,10.0運行,8.0,8.4運行都崩潰了,其他的系統(tǒng)沒有問題,能夠正常運行。報錯信息如下,頓時感覺就不好了。
*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-3347.44.2/UITableView.m:6245
然后進行代碼調(diào)試發(fā)現(xiàn)如下蛋疼的規(guī)律,針對iOS8系統(tǒng)
myTableView.tableFooterView= [UIViewnew];
myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
[myTableViewregisterClass:[MyTableViewCellclass]forCellReuseIdentifier:@"MyTableViewCell"];
這三行代碼不能這么寫,如果順序是當前順序,并且myTableView.separatorStyle=UITableViewCellSeparatorStyleNone,
或者tableview的style = UITableViewStyleGrouped并且myTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLineEtched,會崩潰。其他的情況均沒有問題。
其他情況包括:
1、這三行代碼的順序不是這個順序,隨機組合。假設三句代碼的順序是1,2,3
除了1,2,3會崩潰意外,其他的順序都不會崩潰<1,3,2 3,1,2 3,2,1.......>
2、是當前的順序,但是myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine
3、當前順序刪除掉myTableView.tableFooterView= [UIViewnew];或者刪除掉myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;,或者兩句話都刪除,都不會報錯崩潰。
這個規(guī)律發(fā)現(xiàn)的很糾結(jié),也不知道什么原因。針對iOS8。各路大神有遇到的嗎?麻煩指點一二
參考Demo : https://github.com/RunOfTheSnail/TableViewDemo