滚动后UIcollectionview单元格显示问题

时间:2015-09-15 10:16:23

标签: ios objective-c uicollectionview uicollectionviewlayout

我遇到问题UICollectionView它显得很好但是当我向下滚动并再次向上看时它看起来像这样。 而且它也没有显示所有的细胞。我有20个,但只有18个。

我也在其中使用了分页。之后它会加载10个

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"First";

UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];



AsyncImageView *imageV = (AsyncImageView *)[cell viewWithTag:1];

imageV.accessibilityIdentifier = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
for (UIView *view in imageV.subviews)
{
    if ([view isKindOfClass:[UIImageView class]])
    {
        [view removeFromSuperview];
    }
}

[cell.contentView sendSubviewToBack:imageV];
UILabel *lbl = (UILabel *)[cell viewWithTag:10];
UIButton *addtoorder = (UIButton *)[cell viewWithTag:20];

[addtoorder addTarget:self action:@selector(addtoOrder:) forControlEvents:UIControlEventTouchUpInside];
addtoorder.accessibilityIdentifier = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
addtoorder.userInteractionEnabled = YES;

NSLog(@"dictionary is %@",[brandDetail objectAtIndex:indexPath.row]);

UILabel *cancelprice = (UILabel *)[cell viewWithTag:12];
UIView *discountView = (UIView *)[cell viewWithTag:13];
UILabel *discountpercent = (UILabel *)[cell viewWithTag:11];
UILabel *OFF = (UILabel *)[cell viewWithTag:14];



NSAttributedString * title =
[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Rs. %@",[UtitlityClass addcommaInPrice:[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodretailprice"]]]
              attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[cancelprice setAttributedText:title];
NSLog(@"cancel.text = %@",cancelprice.text);

//   int someFloat = [[[categor.bestsellerlist objectAtIndex:i]valueForKey:@"YouSavePercentage"]intValue];
   // int saving = [[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodretailprice"] intValue] - [[[brandDetail objectAtIndex:indexPath.row] valueForKey:@"prodcalculatedprice"] intValue];
float per = [[[brandDetail objectAtIndex:indexPath.row] valueForKey:@"prodcalculatedprice"] floatValue]/[[[brandDetail objectAtIndex:indexPath.row] valueForKey:@"prodretailprice"] floatValue];
per = per * 100;
per = 100 - per;
int percentage = (int)roundf(per);
//self.savingPrice.text = [NSString stringWithFormat:@"(You save Rs%@)",[UtitlityClass addcommaInPrice:[NSString stringWithFormat:@"%d",saving]]];
NSString *str =  [NSString stringWithFormat:@"%d",percentage];
discountpercent.text =[NSString stringWithFormat:@"%d%%",percentage];

   // NSString *str = [NSString stringWithFormat:@"%@%%",[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"YouSavePercentage"]];

//discountpercent.text =str;
OFF.text = @"OFF";


if ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodallowpurchases"] boolValue])
{

    [addtoorder setTitle:@"Buy Now" forState:UIControlStateNormal];
    [addtoorder setBackgroundColor:[UIColor colorWithRed:163/255.0 green:182/255.0 blue:57/255.0 alpha:1.0]];
}

else
{
    if ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcalculatedprice"] floatValue]<=0)
    {

        [addtoorder setTitle:@"Coming Soon" forState:UIControlStateNormal];
        [addtoorder setBackgroundColor:[UIColor colorWithRed:163/255.0 green:182/255.0 blue:57/255.0 alpha:1.0]];
        cancelprice.hidden = YES;
        discountView.hidden = YES;

    }
    else
    {
        addtoorder.userInteractionEnabled = NO;
        [addtoorder setTitle:@"Out of Stock" forState:UIControlStateNormal];
        [addtoorder setBackgroundColor:[UIColor colorWithRed:208/255.0 green:17/255.0 blue:43/255.0 alpha:1.0]];
    }
}

CGRect fra;
//check for iphone 5/5s/6/6 plus
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
if( screenHeight < screenWidth ){
    screenHeight = screenWidth;
}
if( screenHeight > 480 && screenHeight < 600 ){
    NSLog(@"iPhone 5/5s");
 fra = CGRectMake(9,imageV.frame.origin.y,((([UtitlityClass getScreenWidth]-30)/2)*0.75),((([UtitlityClass getScreenWidth]-30)/2)*0.75));
    imageV.frame = fra;
    NSLog(@"imageView %@",imageV);
    [UtitlityClass getPositionAndSize:imageV];
}
else if ( screenHeight > 600 && screenHeight < 736 ){
        fra = CGRectMake(9,imageV.frame.origin.y,((([UtitlityClass getScreenWidth]-30)/2)*0.75),((([UtitlityClass getScreenWidth]-30)/2)*0.75));
    imageV.frame = fra;
    NSLog(@"imageView %@",imageV);
    [UtitlityClass getPositionAndSize:imageV];


} else if (  screenHeight > 730 && screenHeight < 750 ){
    NSLog(@"iPhone 6 Plus");
        fra = CGRectMake(15,imageV.frame.origin.y,((([UtitlityClass getScreenWidth]-30)/2)*0.60),((([UtitlityClass getScreenWidth]-30)/2)*0.60));
    imageV.frame = fra;
    NSLog(@"imageView %@",imageV);
    [UtitlityClass getPositionAndSize:imageV];
}
else if (screenHeight > 750){
    NSLog(@"iPad");
     fra = CGRectMake(50,imageV.frame.origin.y,147,184);
    imageV.frame = fra;
    NSLog(@"imageView %@",NSStringFromCGRect(imageV.frame));
}


//    NSLog(@"image is %@",[[NSString stringWithFormat:@"%@%@",imageUrl,[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"brandimagefile"]]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);





[imageV loadImageFromURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@%@",imageUrl,[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"imagefilestd"]]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];



//    [imageV addSubview:[UtitlityClass getImage:[[NSString stringWithFormat:@"%@%@",imageUrl,[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"imagefilestd"]]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]:imageV.frame]];




UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellbtn:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[imageV addGestureRecognizer:singleTap];

lbl.text = [[brandDetail objectAtIndex:indexPath.row]valueForKey:@"proddisplayname"];
lbl.frame = CGRectMake(lbl.frame.origin.x,imageV.frame.origin.y+imageV.frame.size.height,lbl.frame.size.width,lbl.frame.size.height);

UILabel *pricel = (UILabel *)[cell viewWithTag:5];
[cell.contentView bringSubviewToFront:pricel];



if (![[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodhideprice"] boolValue])
{
    if ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"OnSalesItem"] isEqualToString:@"0"])
    {




        pricel.text = [NSString stringWithFormat:@"Rs. %@",[UtitlityClass addcommaInPrice:[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcalculatedprice"]]];
        NSLog(@"pricel.text1 = %@",pricel.text);


    }
    else
    {
         pricel.text =[NSString stringWithFormat:@"Rs. %@",[UtitlityClass addcommaInPrice:[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcalculatedprice"]]];
        NSLog(@"pricel.text2 = %@",pricel.text);

    }
    if (([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodretailprice"] floatValue] > 0 )&& ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodretailprice"] floatValue] > [[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcalculatedprice"] floatValue])){
        cancelprice.hidden = NO;
        discountView.hidden = NO;

    }
    else{
        cancelprice.hidden = YES;
        discountView.hidden = YES;

    }

}
else
{
    cancelprice.hidden = YES;
    discountView.hidden = YES;
    pricel.text =[NSString stringWithFormat:@"Rs. %@",[UtitlityClass addcommaInPrice:[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcalculatedprice"]]];
    NSLog(@"pricel.text3 = %@",pricel.text);
}




TPFloatRatingView *ratingView =(TPFloatRatingView *)[cell viewWithTag:34];
ratingView.backgroundColor = [UIColor greenColor];



//check for iphone 5/5s/6/6 plus
if( screenHeight < screenWidth ){
    screenHeight = screenWidth;
}
if( screenHeight > 480 && screenHeight < 600 ){
    NSLog(@"iPhone 5/5s");
    ratingView.frame = CGRectMake(ratingView.frame.origin.x,lbl.frame.origin.y+lbl.frame.size.height,ratingView.frame.size.width,ratingView.frame.size.height);

}
else if ( screenHeight > 600 && screenHeight < 736 ){
    NSLog(@"iPhone 6");
    ratingView.frame = CGRectMake(45,190,ratingView.frame.size.width,ratingView.frame.size.height);
    NSLog(@"Rating View %@",NSStringFromCGRect(ratingView.frame));
    pricel.frame = CGRectMake(40,220,ratingView.frame.size.width+15,ratingView.frame.size.height);
    NSLog(@"pricel %@",NSStringFromCGRect(pricel.frame));
    addtoorder.frame = CGRectMake(addtoorder.frame.origin.x, addtoorder.frame.origin.y, 175, addtoorder.frame.size.height);//cp;
    NSLog(@"addtoorder frame %@",NSStringFromCGRect(addtoorder.frame));


}
else if (  screenHeight > 730 && screenHeight < 750 ){
    NSLog(@"iPhone 6 Plus");
    ratingView.frame = CGRectMake(50,192,ratingView.frame.size.width,ratingView.frame.size.height);
    NSLog(@"Rating View %@",NSStringFromCGRect(ratingView.frame));
    pricel.frame = CGRectMake(0,235,180,30);
    NSLog(@"pricel %@",NSStringFromCGRect(pricel.frame));
    addtoorder.frame = CGRectMake(addtoorder.frame.origin.x, addtoorder.frame.origin.y, 192.59375, addtoorder.frame.size.height);//cp;
    NSLog(@"addtoorder frame %@",NSStringFromCGRect(addtoorder.frame));

}

else if (screenHeight > 750){



    NSLog(@"iPad");
    ratingView.frame = CGRectMake(70,270,90,28);

    NSLog(@"Rating View %@",NSStringFromCGRect(ratingView.frame));
    pricel.frame = CGRectMake(30,330,180,30);
    NSLog(@"pricel %@",NSStringFromCGRect(pricel.frame));
    cancelprice.frame = CGRectMake(30,300,180,20);
    NSLog(@"cancelprice %@",NSStringFromCGRect(cancelprice.frame));

    addtoorder.frame = CGRectMake(0, 370, 240, 53);//cp;
    NSLog(@"addtoorder frame %@",NSStringFromCGRect(addtoorder.frame));
    lbl.frame = CGRectMake(2,187,234,60);
    NSLog(@"lbl frame %@",NSStringFromCGRect(lbl.frame));

}


if ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodhideprice"] boolValue])
{
    pricel.hidden = NO;

    if (!([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"] isEqual:@""]) && !([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"] isEqual:[NSNull null]]) &&!([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"] isEqual:@"null"]))
    {

        addtoorder.userInteractionEnabled = NO;
        [addtoorder setTitle:[NSString stringWithFormat:@"%@",[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"]] forState:UIControlStateNormal];
        if (([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"] isEqual:@"Sold Out"]) || ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"] isEqual:@"Out Of Stock"]) || ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcallforpricinglabel"] isEqual:@"Coming Soon"])) {
            [addtoorder setBackgroundColor:[UIColor colorWithRed:208/255.0 green:17/255.0 blue:43/255.0 alpha:1.0]];
            cancelprice.hidden=YES;
            discountView.hidden = YES;

        }

    }

}

else{
    addtoorder.userInteractionEnabled = YES;
    [UIColor colorWithRed:163/255.0 green:182/255.0 blue:57/255.0 alpha:1.0];
}

if ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcomingsoon"] floatValue]==1)
{

    [addtoorder setTitle:@"Coming Soon" forState:UIControlStateNormal];
    [addtoorder setBackgroundColor:[UIColor colorWithRed:163/255.0 green:182/255.0 blue:57/255.0 alpha:1.0]];
    //cancelprice.hidden = YES;
    //discountView.hidden = YES;

}



ratingView.emptySelectedImage = [UIImage imageNamed:@"stargray"];
ratingView.fullSelectedImage = [UIImage imageNamed:@"stargolden"];
ratingView.contentMode = UIViewContentModeScaleAspectFill;
ratingView.maxRating = 5.0;
ratingView.minRating = 0.0;
ratingView.rating = [[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodratingtotal"]floatValue];
ratingView.editable = NO;
ratingView.halfRatings = YES;
ratingView.floatRatings = YES;
ratingView.backgroundColor = [UIColor clearColor];

if([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodratingtotal"] isEqual:@"0"]){
    ratingView.hidden = YES;
}
if ([[[brandDetail objectAtIndex:indexPath.row]valueForKey:@"prodcalculatedprice"] floatValue]<=0)
{
    pricel.hidden = true;
}

// imageV.tag = indexPath.row;



  return cell;
 }

enter image description here

0 个答案:

没有答案