cell重用時,總是提示找不到標識的cell,讓咱們註冊cell


報錯提示:緩存

[9098:232849] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:6564微信

2016-01-05 00:00:37.574 UI進階考試微信[9098:232849] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier mineCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'app



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 建立可重用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }
    
    return cell;
}


注意:上面的代碼出錯緣由:ide

             1.沒有給storyboard中的cell添加標識。ui

             2.若是是純代碼建立的UIStoryboard,在重用cell的時候,應該使用方法spa

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;



解決方法一:prototype

   在storyboard中給cell添加標識,若是使用這種方法的話,就能夠不用判斷cell是否能從緩存池中找到,由於當從緩存池中找不到的話,就會使用storyboard中的cell。code


解決方法二:orm

   cell重用的時候,使用方法it

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

再加上if判斷

  if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }

當從緩存池中找不到cell的時候,就會if判斷語句的代碼中從新建立。


總結:

   一、若是咱們是用純代碼建立的UITableViewController的話,這時要建立可重用的cell的時候,必定要判斷是否能從緩存池中找到,不然就會報錯的!

   二、若是是在stroryboard中直接以拖拽的方式,建立的UITableViewController的話,最好給storyboard中的cell添加一個標識,以防出錯。標識後咱們就不必再去判斷cell是否能從緩存池中找到,由於即便找不到,系統也會自動的加載storyboard中的cell的。

相關文章
相關標籤/搜索