ios - CollectionView.xib中创建多个nibCell,用的时候如何使用??
天蓬老师
天蓬老师 2017-04-18 09:29:28
0
6
821


如图:我在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
请大神解释啊。。。。。。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (6)
大家讲道理

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

    左手右手慢动作

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

      左手右手慢动作

      Two methods:

      1. split into twoxib,每次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; } }
        大家讲道理

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

          Peter_Zhu

          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?

            黄舟

            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

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!