美文网首页
UITableViewController,代码注册Cell,使

UITableViewController,代码注册Cell,使

作者: ShenYj | 来源:发表于2017-10-27 14:28 被阅读57次

纯代码使用UITableViewController, iOS 8 下在dequeueReusableCellWithIdentifier: forIndexPath:处Crash , iOS 9+无此问题,已经通过代码进行Cell注册

Crash_LOG.png

现解决方案:

  1. 在数据源方法中额外添加注册Cell代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     // 关键代码
    [tableView registerClass:[JSContactMeCell class] forCellReuseIdentifier:kContactMeReusedIdentifier]; // 新增代码用于修复iOS8 Crash
    JSContactMeCell *cell = [tableView dequeueReusableCellWithIdentifier:kContactMeReusedIdentifier forIndexPath:indexPath];
    return cell;
}

2.iOS 8使用 dequeueReusableCellWithIdentifier方法获取Cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (iOS9) {
        JSContactMeCell *cell = [tableView dequeueReusableCellWithIdentifier:kContactMeReusedIdentifier forIndexPath:indexPath];
        return cell;
    }
    JSContactMeCell *cell = [tableView dequeueReusableCellWithIdentifier:kContactMeReusedIdentifier];
    if (cell == nil) {
        cell = [[JSContactMeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kContactMeReusedIdentifier];
    }
    return cell;
}

相关文章

网友评论

      本文标题:UITableViewController,代码注册Cell,使

      本文链接:https://www.haomeiwen.com/subject/oigrpxtx.html