【问题标题】:Objective-C - UICollectionView with wrong indexpathsObjective-C - 具有错误索引路径的 UICollectionView
【发布时间】:2014-12-23 15:15:30
【问题描述】:

我有一个 UICollectionView,我试图将其用作矩阵,其中第一个“列”用作行的名称(颜色),然后在其他项目中放入我要购买的商品数量。我不得不将它分成两个集合,因为标题必须固定在那个位置。

我的问题是最初的情况是这样的:

然后,当我稍微滚动一下灰色集合并使用它时......

应该管理该部分的代码是这样的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

CGSize cellSize = cell.frame.size;
int item = (int)indexPath.item;
int section = (int)indexPath.section;
NSLog(@"S:%d I:%d",section,item);

/*NSMutableArray *array = [matrice objectAtIndex:section];

if([array objectAtIndex:row] != [NSNull null]){
    cell = [[matrice objectAtIndex:section] objectAtIndex:10-row];
    return cell;
}else{
    [[matrice objectAtIndex:section] replaceObjectAtIndex:row withObject:cell];
}*/

if(item == 0){ //Colori
    int labelWidth = cellSize.width - 5;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((cellSize.width/2)-labelWidth/2, (cellSize.height/2)-labelWidth/2, labelWidth, labelWidth)];
    Colore *colore = [cartellaColore.arrayColori objectAtIndex:section];
    [label setText:colore.descrizione];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
    [cell addSubview:label];

    [cell setBackgroundColor:[UIColor whiteColor]];
}
else{
    [cell setBackgroundColor:[UIColor grayColor]];
}

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2*section, 2*item, 35, 10)];
[label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:8]];
[cell addSubview:label];


return cell;

}

我做错了什么?

编辑: 我让程序在同一个单元格上写不同的标签只是为了表明索引路径是错误的。如果它们是正确的,它只会在相同的位置用相同的文本重新添加标签(我知道这仍然是错误的,但它只是为了调试)

【问题讨论】:

    标签: objective-c xcode uicollectionview


    【解决方案1】:

    您不能在 cellForRowAtIndexPath 中添加这样的标签,您必须将 UICollectionViewCell 子类化。

    我在这里对此类问题做出了回应:UICollectionView adding image to a cell

    只需将我解释的内容应用于 UILabel 而不是 UIImageView,您的问题就会消失。

    请告诉我。

    【讨论】:

    • 正如我在这里写下的:我的错:评论部分实际上没有评论,这是为了避免该错误,但仍然混淆了事情。正如你所看到的,如果索引路径是正确的,我只会在标签上加上一个标签,但在同一个地方和相同的文本。所以错误应该在其他地方。
    • 我不知道你的矩阵数组背后隐藏着什么,但我仍然强烈建议你按照我描述的方式去做。
    • 如果你需要一些帮助来理解,我可以写下你需要的一些代码。
    • 我会告诉你的。提前致谢。
    • 我真的不知道为什么我的一切都搞砸了,但按照你的方式行事似乎工作正常。谢谢:-)
    【解决方案2】:

    这是你的错误:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2*section, 2*item, 35, 10)];
    [label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:8]];
    **[cell addSubview:label];**
    

    您在单元格中堆叠标签,该单元格是可重复使用的,并且每次将可重复使用的单元格出列时,您都会为其添加一个标签,因此如果已出列的单元格已经出现,您将为其添加另一个标签,等等。您应该通过检查该单元格是否已经有标签来保护自己免受这种情况的影响到标签,然后当您使单元格出列时,只需检查它是否具有带有该标签的视图,是否已将视图转换为标签并更改其文本属性:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
        if([cell viewWithTag:111]){
            UILabel *label = (UILabe*)[cell viewWithTag:111];
            [label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
        }
        else{
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2*section, 2*item, 35, 10)];
            [label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:8]];
            [label setTag:111];
            [cell addSubview:label];
        }
    

    ...

    【讨论】:

    • 我的错:注释部分实际上没有注释,这是为了避免该错误,但仍然混淆了事情。
    • 但是你为什么要处理单元格出队,collectionView 在内部处理它,这就是为什么你把它放在单元格顶部的行,你只需要处理指定 indexPath 上的正确数据方法也为你提供,我想搞乱出队胜过细胞可重用性的目的
    • 我不想把它搞砸,这就是为什么我在这里问为什么即使我评论那部分它也不起作用。我不应该只有一些标签用这段代码“覆盖”其他标签吗?因为相反,我有在同一个单元格中写不同内容的标签。
    • 不,它们只会放在旧标签之上,而且它们是透明的,因此您也会在它们下方看到旧标签的文本,就像您的图片所示。 indexPath 是您的矩阵导航器,因此只需查找行/节坐标上的数据并用该数据填充您的出列单元格,并保护自己免受堆叠标签的影响,就像我向您展示的那样,您会没事的..
    • 是的,但是如果索引路径相同,我应该在相同的坐标中使用相同的文本。所以,即使那是透明的,我也应该只看到一个标签。
    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2018-03-05
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多