2017-09-08 9 views
0

json을 사용하여 데이터베이스의 아이콘으로 하위 카테고리를 표시하는 collectionView가 있습니다.하지만 잘 작동하지만 최근에는 큰 문제가 있습니다. 데이터베이스의 값이 "intColumns"와 같으면 값이 2이고 "intColumns = 3"인 경우 잘 나타납니다. 그러나 database = 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가 반환 :

(210)

또는

return 5/3; 

그래서 ... 5/3은 무엇

? 당신이 정수 나누기를하고 있기 때문에 그것은 1입니다. 섹션 수에 1.666666을 반환 하시겠습니까? 수행

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

좋은 생각,하지만 당신은 5 개 값이있는 경우 여전히 ... 당신이 좀 더 정보를 제공해야합니다 다음 문제 –

+0

이 있습니다

대부분의 경우, numberOfSectionsInCollectionView를 들어, 당신이 원하는 당신은 2 개의 단면을 얻지 만 3 개의 세포의 단지 1 개의 행을 보지 않습니까? 1 섹션 만 보시겠습니까? 디버그를 통해 기대했던 가치를 얻고 있는지 살펴 보았습니까? – DonMag

+0

예 3 셀의 행이 하나 밖에없고 2 셀이 누락 된 경우 [categoryArray count]가 홀수 인 경우 1을 더하십시오. 그것을하는 방법? –