繁体   English   中英

如何在collectionview中加载collectionivew单元的不同笔尖?

[英]How to load different nibs of collectionivew cell in collectionview?

我有一个表视图,在此表视图中,我有四行,在每一行中,我以编程方式添加了一个集合视图,并且检查是否如果我的第一行应该注册集合视图单元格的第一个笔尖,如果它是我的第二行,则应该注册收集视图单元格的第二个笔尖,依此类推,然后我简单地重新加载了收集视图。 我用下面的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellID"];

if(cell==nil){
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
collectionView.dataSource=self;
collectionView.delegate=self;


if (indexPath.row==0) {
    [collectionView registerNib:[UINib nibWithNibName:@"QuotesCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"quotes"];

} else if (indexPath.row==1){

    [collectionView registerNib:[UINib nibWithNibName:@"AlphabeticalWiseCategoriresCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"alphabets"];

} else if (indexPath.row==2){

    [collectionView registerNib:[UINib nibWithNibName:@"DaysCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"days"];

}else if(indexPath.row==3){

    [collectionView registerNib:[UINib nibWithNibName:@"LifeAspectsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"life"];
}

[collectionView reloadData];
return cell;

}

我的问题/问题是,我怎么可能会知道与我有以下代表来处理该集合视图笔尖,因为在这里我要说明,同时与集合视图注册笔尖,我已经使用了相同的重用标识符?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


}

我的第二个问题是,由于我具有不同的集合视图,我如何才能检测到单击了哪个表视图行的集合视图单元格?

您可以将每次创建的collectionView保留在array 因此, array中每个collectionView的索引将等于indexPath.row 通过在- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath检索collectionView参数的索引,您可以知道要处理的collectionView以及UICollectionViewCell

首先,为什么不使用静态表视图? 如果您对每个单元格进行硬编码,我认为它将是很好的增强。 接下来是您的问题,iOS提供了每个UIView标签属性。 如果将表重新制作为静态表,则可以通过InterfaceBuilder设置标签属性。 如果不:

UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
collectionView.dataSource=self;
collectionView.delegate=self;
collectionView.tag = indexPath.row;

然后:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  switch collectionView.tag {
  case 0:
    ...
    break
  case 1:
    ...
    break
  }
}

您可以使用这种方法来解决第二个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM