CollectionView显示单元格是否等于int Columns值

时间:2017-09-08 18:56:27

标签: ios objective-c uicollectionview xcode8

我有一个collectionView使用json显示带有来自数据库的图标的子类别它工作正常但是最近注意到它有一个大问题。如果数据库中的值等于" intColumns"它可以正常工作,例如,如果值2和" intColumns = 3"它显示出注意到。但如果数据库中的值= 3则显示3个单元格。

如果数据库中的值= 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;

}

1 个答案:

答案 0 :(得分: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);