当我使用IOS UICollectionView时,我想在单元格

时间:2017-01-13 03:53:18

标签: ios uicollectionview uicollectionviewcell

您好我正在为IPhone编写代码,所以我使用的是UICollectionView,我希望在单元格上添加更多自定义的UIView。但是我认为单元格被多次调用所以总视图就像这样奇怪。

[细胞的结果] [1]

看,小方块是自定义的UIView,但每个单元格的颜色不同。因为当UICollectionView占用单元格时,它会多次调用单元格,因此定制的UIView绘制框多次,其颜色变得越来越厚。

以下是单元格的简要代码。我有这么多尝试。但每次我都失败了。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DumpItForIOSReusableCollectionViewCell * cell = (DumpItForIOSReusableCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSString *cellObjKey = [NSString stringWithFormat:@"%d", (unsigned int)indexPath.row];
    NSString *strFlot = (NSString *)([ko objectForKey:cellObjKey]);
    CGFloat curCont = (CGFloat)[strFlot  floatValue];
    [[cell innerUIView] setParentObj:parentObj];
    [cell imageViewHeightLayoutConstraint].constant =  curCont;
    [[cell innerUIView] setCellIndex:indexPath.row];
    [[cell innerUIView] setHeightDictionary:[self dataList]];


    NSString *itemNm = ((DumpItForIOSItemObject *)[_dataList objectAtIndex:indexPath.row]).itemTitle;
    NSString *itemDesc = ((DumpItForIOSItemObject *)[_dataList objectAtIndex:indexPath.row]).itemDescription;

    //NSLog(@ "cellid = %d, in = %@, x= %f, y= %f,h = %f %p" ,(unsigned int)indexPath.row,itemNm,[cell cImgView].frame.origin.x,[cell cImgView].frame.origin.y,curCont,[cell innerUIView]);

    [[cell cImgView] setImage:[UIImage imageNamed:((DumpItForIOSItemObject *)[_dataList objectAtIndex:indexPath.row]).itemImage]];
    [[cell cLabel] setText:itemNm];
    [[cell cmLabel] setText:((DumpItForIOSItemObject *)[_dataList objectAtIndex:indexPath.row]).itemDescription];
    NSString *checkId = ((DumpItForIOSItemObject *)[_dataList objectAtIndex:indexPath.row]).itemImageViewContainerKey;

     @synchronized (cell) {

            PriceView *pv = (PriceView *)([avs objectForKey:cellObjKey]);
            if(!pv){
                pv = [[PriceView alloc]init];
                [pv setOpaque:NO];
                [pv setFrame:CGRectMake(0, 0, 100, 100)];
                [avs setObject:pv forKey:cellObjKey];
                [[cell cImgView] addSubview:pv];
            }else{

            }//if
            //[pv removeFromSuperview];

            NSArray *viewsToRemove = [cell subviews];
            for (NSObject *v in viewsToRemove) {
                if([v isKindOfClass:[PriceView class]]) [(PriceView *)v removeFromSuperview];
            }//for

            [pv setCalledNumber:0];
            [[cell cImgView] addSubview:pv];
        }

    return cell;
}

有没有只想添加子视图的想法? 提前致谢。

============我以不同的方式解决了这个问题===========

[已解决的结果]

https://i.stack.imgur.com/klSTi.png

0 个答案:

没有答案