2017-09-08 1 views
0

Ich habe eine CollectionView, um Unterkategorien mit Symbolen aus der Datenbank mit Json anzuzeigen, es funktioniert gut, aber vor kurzem hat es ein großes Problem. Wenn Werte in der Datenbank gleich "intColumns" sind, funktioniert es gut, wenn zum Beispiel Wert 2 und "intColumns = 3" es noting anzeigt. aber wenn Werte in Datenbank = 3 zeigt es die 3 Zellen.CollectionView zeige Zellen wenn gleich zu int Spalten Wert

und wenn Werte in Datenbank = 5 zeigt es nur 3 Zellen.

Was ist das Problem hier?

- (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; 

}

Antwort

0

Sie haben:

intColumns = 3 

So numberOfItemsInSection kehrt 3

Dann, wenn [categoryArray count]5 gleich, Ihr numberOfSectionsInCollectionView zurückkehrt:

return [categoryArray count]/intColumns; 

oder

return 5/3; 

Also ... was ist 5/3? Es ist 1, weil Sie Integer-Division machen. Möchten Sie 1.666666 für die Anzahl der Abschnitte zurückgeben?

Höchstwahrscheinlich für numberOfSectionsInCollectionView, mögen Sie:

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

gute Idee, aber immer noch das Problem –

+0

Dann werden Sie ein bisschen mehr Informationen zur Verfügung stellen müssen ... Wenn Sie 5 Werte haben, tun Du bekommst 2 Abschnitte, siehst aber nur 1 Zeile mit 3 Zellen? Siehst du nur einen Abschnitt? Hast du im Debug debattiert, um zu sehen, ob du die Werte bekommst, die du erwartest? – DonMag

+0

Ja, ich bekomme nur eine Zeile mit 3 Zellen und 2 Zellen fehlt, was ist mit add 1 wenn [categoryArray count] ungerade Zahl ist. wie es geht? –

Verwandte Themen