search

Home  >  Q&A  >  body text

ios - CollectionView.xib中创建多个nibCell,用的时候如何使用??


如图:我在collection.xib中创建了2个cell,定义了不同的identifier,

使用的时候:

     NSArray *nibArrays = [[UINib nibWithNibName:@"LongCell" bundle:nil] instantiateWithOwner:nil options:nil];
    [collectionView registerClass:[[nibArrays objectAtIndex:0] class] forCellWithReuseIdentifier:@"LongCellFirst"];
    [collectionView registerClass:[[nibArrays objectAtIndex:1] class] forCellWithReuseIdentifier:@"LongCellSecond"];
#pragma mark - UICollectionViewDelegate DataSource -
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item % 2 == 0) {
        LongCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LongCellFirst" forIndexPath:indexPath];

        return cell;
    }else {
        LongCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LongCellSecond" forIndexPath:indexPath];
        return cell;
    }
}

这样能运行,但是取出的cell都是上面什么也没有。
是我注册cell不对吗??还是因为不能这样用?tableViewCell.xib就可以这样搞啊?
tableView.xib中创建多个cell
请大神解释啊。。。。。。

天蓬老师天蓬老师2772 days ago853

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 09:31:28

    You created it using nib, shouldn't you use registerNib to register it? Why use regClass.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:31:28

    The xib file was read incorrectly
    NSArray *nibArrays=[[NSBundle mainBundle]loadNibNamed:@"LongCell" owner:nil options:nil];

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:31:28

    Two methods:

    1. split into two xib ,每次 registerNib: one;

    2. Synthesize one, but initialize the instance yourself:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if (indexPath.row % 2) {
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LongCellFirst"];
              if (!cell) {
                  cell = [[UINib nibWithNibName:@"LongCell" bundle:nil] instantiateWithOwner:self
                                                                                     options:nil].firstObject;
              }
      
              return cell;
          }
          else {
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LongCellSecond"];
              if (!cell) {
                  cell = [[UINib nibWithNibName:@"LongCell" bundle:nil] instantiateWithOwner:self
                                                                                     options:nil].lastObject;
              }
      
              return cell;
          }
      }

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:31:28

    It is recommended that one xib file corresponds to one View, there is no need to save files

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:31:28

    According to the method mentioned above, the cell can be displayed, but the control settings in the cell have no effect. Has the author solved the problem?

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:31:28

    When associating the controls on the cell in the cell, if you don’t make any changes. All your associated controls default to controls on cell1. In layman's terms, when connecting, the controls on the first cell are used by default

    reply
    0
  • Cancelreply