Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 根据indexPath自定义UICollectionViewCell_Ios_Customization_Uicollectionviewcell - Fatal编程技术网

Ios 根据indexPath自定义UICollectionViewCell

Ios 根据indexPath自定义UICollectionViewCell,ios,customization,uicollectionviewcell,Ios,Customization,Uicollectionviewcell,嗨,我正试图根据其indexPath自定义我的collectionviewcell,但如果我设置了 if (indexPath.row == 0) { [cell addSubView: view]; } 视图在某些单元格中随机显示。 这是我正在使用的代码 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { retu

嗨,我正试图根据其indexPath自定义我的collectionviewcell,但如果我设置了

if (indexPath.row == 0)
{
     [cell addSubView: view];
}

视图在某些单元格中随机显示。 这是我正在使用的代码

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 15;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    NSInteger row = indexPath.row;
    UIView *contentCell = [[UIView alloc] initWithFrame:cell.frame];
    if (row == 0)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(1.0f, 1.0f, 50.0f, 50.0f)];
        label.text = @"Test";
        [contentCell addSubview:label];
    }
    [cell addSubview:contentCell];
    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"container"]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d", indexPath.row);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(414, 228);
}

您多次将视图作为子视图添加。由于“dequeueReusableCellWithReuseIdentifier”,单元格被重用,所以在上下滚动之后,您将返回现有视图,该视图已经添加了子视图

您应该阅读更多关于重用单元格的内容

避免这种行为的一种方法是在创建单元时创建所有视图,并仅显示/隐藏它们。
否则,为不同的行创建具有不同标识符的单元格-对于第0行,您必须阅读有关可重用单元格的内容

这样做可以暂时避免问题

if (row == 0)
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(1.0f, 1.0f, 50.0f, 50.0f)];
    label.text = @"Test";
    label.tag = 200;
    [contentCell addSubview:label];
}else{
    UIView* lbl = [contentCell viewWithTag:200];
    if(lbl)
       [lbl removeFromSuperView];
}

但这会影响滚动和内存性能,您可以在默认情况下将标签放在单元格中&&show/hide它在if/else块中

“视图在某些单元格中随机显示”,而不仅仅是在firsttableview中,单元格被重用。因此,您需要清除不需要的单元格中的标签。向单元格中添加子视图的方式不是一个好主意。如果无法检查indexPath,如何显示/隐藏它们?可以基于indexPath进行隐藏/显示,但不能基于此添加子视图。您还可以添加子视图并向其添加“标记”,并始终检查它是否存在以及是否需要添加。谢谢。我需要设置所有15个单元格的内容。我该怎么做?我必须对所有单元格进行此检查?要仅更改内容1-首先,检查标签是否在之前创建2-如果为零则创建3-每次更改文本UILabel*标签=(UILabel*)[contentCell viewWithTag:200];如果(label==nil){label=[[UILabel alloc]initWithFrame:CGRectMake(1.0f,1.0f,50.0f,50.0f)];label.tag=200;[contentCell addSubview:label];}label.text=@“测试”//每个单元格的任何文本