UITableViewCell 注册与不注册

2017-12-27  本文已影响0人  benzhaoguo

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];

    }

上一篇下一篇

猜你喜欢

热点阅读