ios - ipod 中的 Collectionview 问题

标签 ios iphone objective-c uicollectionview

我的项目中有 Collectionview...它可以在 ipad 和 iphone 中正常工作..但不能在 ipod 中...ipod id IOS7 的版本..任何人都可以告诉我问题是什么..我必须做什么做... 我的代码如下

     _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(-5, 0,       self.view.bounds.size.width, height) collectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    [_collectionView registerClass:[UICollectionViewCell class]    forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor whiteColor]];
    [_collectionView setShowsHorizontalScrollIndicator:NO];
    [_collectionView setShowsVerticalScrollIndicator:NO];

    [self.view addSubview:_collectionView];


     - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
     {
    return arrayMonths.count;
     }

// 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];
       for(UIView *subview in [cell subviews]) {
        [subview removeFromSuperview];
       }
       MonthView *month=[arrayMonths objectAtIndex:indexPath.row];
       if ([month.strMonthName isEqualToString:@"February"]) {
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, -5, cell.frame.size.width, 40)];
        [label setText:[NSString stringWithFormat:@"%@ - %@",[[NSUserDefaults standardUserDefaults] valueForKey:@"KiCalName"],month.strYear]];
        label.textAlignment=NSTextAlignmentCenter;
        [label setFont:[UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]];
        [cell addSubview:label];
        [month setFrame:CGRectMake(0, 35, month.frame.size.width, month.frame.size.height)];
        }else if([month.strMonthName isEqualToString:@"January"] || [month.strMonthName  isEqualToString:@"March"]){
        [month setFrame:CGRectMake(0, 35, month.frame.size.width, month.frame.size.height)];
        }
       [cell addSubview:month];
       [hud hide:YES];
       return cell;
        }

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MonthView *monthOld=[arrayMonths objectAtIndex:indexPath.row];
    CGRect rectNew=CGRectMake(monthOld.frame.origin.x, monthOld.frame.origin.y, monthOld.frame.size.width+40, monthOld.frame.size.height+40);
    MonthView *month=[[MonthView alloc]initWithFrame:monthOld.frame andYear:monthOld.strYear andMonth:monthOld.strMonthName andNoOfDays:monthOld.noOfDays andArrayOffDays:monthOld.arrayOFF andArrayOnDays:monthOld.arrayONN];
    [month setBackgroundColor:[UIColor whiteColor]];
    UIView *view=[[UIView alloc]initWithFrame:rectNew];
    [view setBackgroundColor:[UIColor whiteColor]];
    [month setFrame:CGRectMake(20, 20, month.frame.size.width, month.frame.size.height)];
    [view addSubview:month];
    [[KGModal sharedInstance] setShowCloseButton:NO];
    [[KGModal sharedInstance] showWithContentView:view andAnimated:NO];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    MonthView *month=[arrayMonths objectAtIndex:indexPath.row];
   NSLog(@"month name %@",month.strMonthName);
   NSLog(@"month %@",month);
   CGSize height;
   height=CGSizeMake(105, 155);
    if ([month.strMonthName isEqualToString:@"January"]) {
        height=CGSizeMake(105, 190);
        NSLog(@"called");
    }else if ([month.strMonthName isEqualToString:@"February"]){
        height=CGSizeMake(105, 190);
        NSLog(@"called");
    }else if ([month.strMonthName isEqualToString:@"March"]){
        height=CGSizeMake(105, 190);
        NSLog(@"called");
    }else{

    }
    return height;
}


-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

最佳答案

问题是您正在使单元出列,

UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

问题是你没有检查 nil..!!!

使用以下条件,

if(cell==nil)
{
//then alloc cell
}

关于ios - ipod 中的 Collectionview 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673958/

相关文章:

objective-c - 在 iOS 中播放声音会暂停背景音乐

objective-c - Swift 转换为数组和字典

ios - UIView 似乎不记得它的标签

iphone - 将NSStrings转换为NSDates时如何避免语言问题?

ios - 如何在iOS objective-c 中打开链接时启动Chrome浏览器

php - 从 PHP 获取数据到 iOS 应用程序的最佳方式是什么?

objective-c - CGLayerRef 仅在 OS X 10.11 (El Capitan) 中显示为空

ios - 如何在 AspectFit 比例后调整 UIImageView 的大小

ios - 使用自签名 SSL 证书访问开发服务器

iphone - MonoTouch WIFI SSID