如图:我在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
请大神解释啊。。。。。。
大家讲道理2017-04-18 09:31:28
You created it using nib, shouldn't you use registerNib to register it? Why use regClass.
ringa_lee2017-04-18 09:31:28
The xib file was read incorrectly
NSArray *nibArrays=[[NSBundle mainBundle]loadNibNamed:@"LongCell" owner:nil options:nil];
ringa_lee2017-04-18 09:31:28
Two methods:
split into two xib
,每次 registerNib:
one;
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;
}
}
大家讲道理2017-04-18 09:31:28
It is recommended that one xib file corresponds to one View, there is no need to save files
天蓬老师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?
黄舟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