2017-09-08 13 views
0

jsonを使用してデータベースのアイコンを持つサブカテゴリを表示するcollectionViewがありますが、それはうまくいきますが、最近大きな問題があります。データベースの値が "intColumns"に等しい場合、値2と "intColumns = 3"の場合はそれがうまく機能します。データベースの値が3の場合は3つのセルが表示されます。CollectionViewはint​​と等しい場合にセルを表示します。列の値

データベースの値が5の場合、3つのセルしか表示されません。

ここで問題は何ですか?

- (void)viewDidLoad { 
[super viewDidLoad]; 
[[self navigationController] setNavigationBarHidden:NO animated:YES]; 
self.navigationController.navigationBar.translucent = NO; 


GADRequest *request = [GADRequest request]; 
// Enable test ads on simulators. 
request.testDevices = @[ GAD_SIMULATOR_ID ]; 

windowSize = [[UIScreen mainScreen] applicationFrame]; 
common = [[CommonFramework alloc]init]; 

categoryArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:[[NSString alloc] initWithFormat:@"%@%@",@"subcategory",[[NSUserDefaults standardUserDefaults]objectForKey:@"selectedMainCategory"]]]]; 
selectedCategory = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults]objectForKey:@"selectedMainCategory"]]; 
selectedCategoryIndex = [[NSString alloc] initWithString:[selectedCategory objectForKey:@"id"]]; 
[self loadCategory]; 
// Do any additional setup after loading the view from its nib. 
intColumns = 3; 

self.navigationItem.title = [selectedCategory valueForKey:@"title"]; 
[self.gridView registerClass:[GridCell class] 
forCellWithReuseIdentifier:@"GridCell"]; 
[self loadCategory]; 


} 


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

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView 
*)collectionView { 
return [categoryArray count]/intColumns; 
} 


- (CGSize)collectionView:(UICollectionView *)collectionView layout: 
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: 
(NSIndexPath *)indexPath { 
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] 
statusBarOrientation])) { 
    return CGSizeMake(windowSize.size.width/intColumns*2, 100); 
} 
//NSLog(@"Grid Width : %i", round(_gridView.frame.size.width /intColumns)); 

return CGSizeMake(windowSize.size.width/intColumns - 20 , 100.f); 
} 



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
// Setup cell identifier 
static NSString *cellIdentifier = @"GridCell"; 

GridCell *cell = [collectionView 
dequeueReusableCellWithReuseIdentifier:cellIdentifier 
forIndexPath:indexPath]; 
CGRect cellFrame = cell.frame; 
cellFrame.size.width = windowSize.size.width/intColumns -20; 
cellFrame.size.height = 100; 
cell.frame = cellFrame; 
if ([cell subviews]){ 
    for (UIView *subview in [cell subviews]) { 
     [subview removeFromSuperview]; 
    } 
} 

int itemIndex = indexPath.row + (intColumns * indexPath.section); 

UIView *CellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 
cell.frame.size.width, cell.frame.size.height)]; 

UIImageView *imgIcon = [[UIImageView 
alloc]initWithFrame:CGRectMake((CellView.frame.size.width/2) - (30), 0, 60, 
60)]; 



    NSString *documentsDir = [NSSearchPathForDirectoriesInDomains 
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

NSString *imagePath = [documentsDir stringByAppendingPathComponent: 
[NSString stringWithFormat:@"%@",[[categoryArray objectAtIndex:itemIndex] 
valueForKey:@"icon"]]]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
bool success = [fileManager fileExistsAtPath: imagePath]; 

if (!success) 
{ 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
        ^{ 
         UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",common.IMAGE_ICON,[[categoryArray objectAtIndex:itemIndex] valueForKey:@"icon"]]]]]; 



         dispatch_async(dispatch_get_main_queue(), 
             ^{ 
              if(image!=nil) 
              { 
               NSString *documentsDir = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
               NSLog(@"Image Path : %@",common.IMAGE_ICON); 
               //[UIImageJPEGRepresentation(image, 1.0) writeToFile:imagePath options:NSAtomicWrite error:nil]; 

               [self addSkipBackupAttributeToPath:[documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",common.IMAGE_ICON,[[categoryArray objectAtIndex:itemIndex] valueForKey:@"icon"]]]]; 

               [imgIcon setImage:image]; 

               //             [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
              } 
             }); 
        }); 
} 
else 
{ 
    [imgIcon setImage: [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imagePath]]]]; 
} 




[CellView addSubview:imgIcon]; 

// NSLog(@"Row - %i Sec - %i",indexPath.row,indexPath.section); 

NSString *title = [[categoryArray objectAtIndex:itemIndex] objectForKey:@"title"]; 

UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,65, CellView.frame.size.width, 18)]; 
fromLabel.text = title; 
fromLabel.textAlignment = NSTextAlignmentCenter; 
fromLabel.font = [UIFont fontWithName:@"HelveticaNeueW23foSKY-Bd" size:12.0 ]; 
//[fromLabel setFont:[UIFont fontWithName:@"Aral" size:10]]; 
[CellView addSubview:fromLabel]; 

[cell addSubview:CellView]; 

// Return the cell 
return cell; 

}

答えて

0

あなたは持っている:

intColumns = 3 

のでnumberOfItemsInSection戻りその後3

[categoryArray count]は、5等しい場合、あなたはnumberOfSectionsInCollectionViewが戻っている:

return [categoryArray count]/intColumns; 

または

return 5/3; 

そう... 5/3は何ですか?整数除算を行っているので、1です。セクション数に1.666666を返しますか?

ほとんどの場合、numberOfSectionsInCollectionViewのために、あなたがしたい:

return ceil((double)[categoryArray count]/(double)intColumns); 
+0

良いアイデアを、まだ問題 –

+0

を持って、あなたが少しより多くの情報を提供する必要があります...あなたは5つの値を持っている場合は、行います2つのセクションがありますが、3つのセルの1つの行だけが表示されますか? 1つのセクションしか表示されませんか?あなたが期待している値を取得しているかどうかを確認するためにデバッグを始めましたか? – DonMag

+0

はい、3つのセルから1行しか得られず、2つのセルがない場合、[categoryArray count]が奇数の場合は1を加算します。どうやってするの? –

関連する問題