iOS 中cell的几种方式创建

2019-10-30  本文已影响0人  leisdelta

一.xib注册方式1

// viewDidLoad中写

UINib*nib = [UINibnibWithNibName:NSStringFromClass([TopicCell class]) bundle:nil];

[self.tableViewregisterNib:nib forCellReuseIdentifier:TopicCellId];

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

 TopicCell*cell = [tableView dequeueReusableCellWithIdentifier:TopicCellId];

cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];

 returncell;

}

二.xib注册方式2(没有在viewDidLoad中加载xib的话)

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

 TopicCell*cell = [tableView dequeueReusableCellWithIdentifier:TopicCellId];

if(cell == nil){

    cell = [[NSBundlemainBundle] loadNibNamed:NSStringFromClass([TopicCellclass]) owner:nil    options:nil].firstObject;

    }

cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];

 returncell;

}

三.没有xib的注册方式3

3.1在viewdidload里面加上代码

[self.tableViewregisterClass:[UITableViewCellclass] forCellReuseIdentifier:ID];

3.2在UITableViewCell里面加入如下代码

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

 UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:ID];

    cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];

 returncell;

}

四.没有xib的注册方式4

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     NSLog(@"cellForRowAtIndexPath");

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID”];

    If(cell == nil){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];   

  }

      cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

    return cell;

}

上一篇 下一篇

猜你喜欢

热点阅读