美文网首页ios开发布局iOS开发指南
关于TableViewCell注册和不注册复用的问题

关于TableViewCell注册和不注册复用的问题

作者: 这小子 | 来源:发表于2017-04-26 18:12 被阅读2947次

    当我们使用tableViewCell时,一般会对cell进行复用,来减少cell创建的压力,但是有时候分不清注册cell和不注册复用cell,导致乱用现象发生,本文专门区分了cell的注册和不注册的复用问题

    Cell注册的两种方式
    1.tableView registerNib:(nullable UINib *) forCellReuseIdentifier:(nonnull NSString *)
    2.tableView registerClass:(nullable Class) forCellReuseIdentifier:(nonnull NSString *)
    
    Cell注册的形式:
    
    (1)系统cell
        1.注册
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        2.不注册
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    
    (2)自定义cell
        1.注册
        [self.tableView registerClass:[xxxxCell class] forCellReuseIdentifier:@"cell"];
        xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
      2.不注册
        xxxxCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
      if (cell==nil) {
          cell=[[xxxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    (3)自定义cellXib注册
        1.注册
        [tableView registerNib:[UINib nibWithNibName:@"xxxxViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
        xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        2.不注册
         xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@“xxxxCell" owner:self options:nil]lastObject];
        }
    

    切记不可将注册和不注册混用,导致四不像

    相关文章

      网友评论

        本文标题:关于TableViewCell注册和不注册复用的问题

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