0

I have some json data which have some documents.It have 4 section -in each section it have some data or no data.I need to display the data of each section

My question is:

  1. I used NSDictionary to store all section data in one array and i tried to display.But it not showing my data
  2. In each section the title should be ( eg: white(0-data), Black(3-data) like that).That i tried ,but din get that.
user5513630
  • 1,709
  • 8
  • 24
  • 48
  • What result are you currently getting? It's hard to help if we don't know exactly what the problem is. You've shown us your code and told us what you want to achieve, which is good, but you've neglected to tell us what stage you're at. – Matthew Hallatt Nov 03 '15 at 11:04
  • My data are not display only section headers alone shows without data.see my post i have added my screen how i am getting output – user5513630 Nov 03 '15 at 11:07
  • What value are you getting in `numberOfItemsInSection:`? Can you confirm whether or not the `arrayPDFName` has data at that point? – Matthew Hallatt Nov 03 '15 at 11:09
  • you should handle your data in `numberOfItemsInSection` for example : if (section == 0) { return N; } any way you should handle to show how many data in each section from your data – Mo Farhand Nov 03 '15 at 11:11
  • Are you reloading you collection view after `getdata` completed? – Vijay Nov 03 '15 at 11:15
  • @Matthew Hallatt actually i am new to handle ios. so only i am not understand what you are asking for?? – user5513630 Nov 03 '15 at 11:16
  • please if any wrong in my code or any correction please explain in code to get my data..so that i can learn what i have missed and it will help to correct next time – user5513630 Nov 03 '15 at 11:18
  • @user5513630 Before the line: `return arrayPDFName.count;` in `numberOfItemsInSection:` add `NSLog(@"%i", arrayPDFName.count);` and see what you're actually getting there. My bet is arrayPDFName won't contain any data. – Matthew Hallatt Nov 03 '15 at 11:18
  • yes ,i am geting 0 0 0 0 – user5513630 Nov 03 '15 at 11:21
  • @ Matthew Hallatt ohh But i also save my json in plist.When i put NSLog to see the data i am getting all my all section data in Json format.But why my arrayPDFNmae is 0 0 0 0.Please help me clear it out – user5513630 Nov 03 '15 at 11:25
  • `One more information` --- i try to display my `blue section` data which has `3 files`.That time its work and it print` 3`.But when i add other section and combined to use my arrayPDFName data is `0 0 0 0` – user5513630 Nov 03 '15 at 11:29
  • see my updated content where i have added extra section to combine and store in array – user5513630 Nov 03 '15 at 11:31
  • @Vijay Please see my updated post i a doing wrong i that 5 line to add all section data in to array and to add with arrayPDFName – user5513630 Nov 03 '15 at 11:41
  • no it getting error `expection identifier` – user5513630 Nov 03 '15 at 11:50
  • @user5513630 Sorry, Try like this `NSArray *arrayFiles = @[dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4];` – Vijay Nov 03 '15 at 11:54
  • i am getting 4 4 4 4 data in my each 4 section,,I have 37 file in one section only.others section dont have data – user5513630 Nov 03 '15 at 11:59
  • @user5513630, You have to maintain each section title as array, those arrays are added to `arrayPDFName` array. In `numberOfItemsInSection`, get the array from `arrayPDFName` and return the count of array – Vijay Nov 03 '15 at 12:15
  • @Vijay i am using search bar so ..how can i return value ..Please explain me in code.I new bie to code – user5513630 Nov 03 '15 at 12:22
  • @user5513630 Check my answer below. do as it is and let me know. – Vijay Nov 03 '15 at 12:38
  • @vijay please try to give solution bro http://stackoverflow.com/questions/33505951/not-able-to-search-the-data?noredirect=1#comment54796661_33505951 – user5513630 Nov 04 '15 at 05:45
  • @vijay please try to give solution bro http://stackoverflow.com/questions/33505951/not-able-to-search-the-data?noredirect=1#comment54796661_33505951 – user5513630 Nov 04 '15 at 05:45

2 Answers2

2

There are some changes needs to be done with your code. Try the below.

In viewDidLoad,

[self getdata];
[self.mycollectionView reloadData];

In getdata method,

NSArray *arrayFiles = [NSArray arrayWithObjects: dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4, nil];

for (NSDictionary *dict in arrayFiles) {
  NSMutableArray *arr = [NSMutableArray array];
  NSArray *a = dict[@"files"];
  for(int i=0; i < a.count; i ++) {
      NSString *strName = [NSString stringWithFormat:@"%@",[[dict[@"files"] objectAtIndex:i] valueForKey:@"name"]];
      NSLog(@"str: %@", strName);
      [arr addObject:strName];
   }  
  [arrayPDFName addObject:arr];
}

In numberOfItemsInSection,

return ((NSArray *)arrayPDFName[section]).count;

In cellForRowAtIndexPath,

cell.myLabel.text = arrayPDFName[indexPath.section][indexPath.row];

Do this for section title

  1. Have one mutable array as titleArray or something.
  2. In viewdidload, titleArray = [NSMutableArray array];

In getdata method,

NSDictionary *dictOriginal = jsonResults[@“white”]; 
[titleArray addObject:[NSString stringWithFormat:@"white(%d)", dictOriginal[@"count"]]];

Do the same for dictOriginal2, dictOriginal3, dictOriginal4.

In viewForSupplementaryElementOfKind,

header.myHeaderLabel.text = titleArray[indexPath.section]

In numberOfSectionsInCollectionView,

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
  return titleArray.count; 
}
Vijay
  • 791
  • 1
  • 8
  • 23
  • i am getting error in two place error is :` Property 'count' not found on object of type 'id _Nullable'` in `for(int i=0; i < dict[@"files"].count; i ++) {` and `return arrayPDFName[section].count;` – user5513630 Nov 03 '15 at 12:50
  • Try this `NSArray *a = dict[@"files"]; for(int i=0; i < a.count; i ++) {` – Vijay Nov 03 '15 at 12:59
  • will check out bro.... and i have asked 2 question in my post 1. how to change my section name with appropriate name with inside number of data.....eg: ( blue(37), white(0), black(0), orange (0) ) like this 2. in my remaining 3 section i am not having data so i need to show use no data with same cell. – user5513630 Nov 03 '15 at 13:05
  • @user5513630, Yes. We can go one by one. First check changes above and let me know. Edited my answer too. – Vijay Nov 03 '15 at 13:08
  • yes it worked .....with each section perfect bro...but my search bar is not fetching data ..ok first clear me that 2 question i have asked..then i will post new question for my search bar bro – user5513630 Nov 03 '15 at 13:12
  • Great. You need to change like this `((NSArray *)arrayPDFName[section]).count` in `numberOfItemsInSection` too. Let me check your question. – Vijay Nov 03 '15 at 13:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94097/discussion-between-vijay-and-user5513630). – Vijay Nov 03 '15 at 13:15
1

It seems like you might be setting up your collection view before you have any data.

If the log I told you to put in is returning 0, 0, 0, 0 every time, then arrayPDFName is empty.

Try calling [self.collectionView reloadData]; at the end of your getdata method.

If that's not the issue, then there is likely a problem with how you're adding the items to the arrayPDFName array.

EDIT:

Try:

NSArray *arr = [NSArray arrayWithObjects: dictOriginal[@"files"], dictOriginal2[@"files"], dictOriginal3[@"files"], dictOriginal4[@"files"], nil];

EDIT:

You're seeing 4 items in each section because you're returning arrayPDFName.count; in numberOfItemsInSection:.

Instead, you want to return the count of the files inside:

return arrayPDFName[section][@"count"];

Matthew Hallatt
  • 1,310
  • 12
  • 24
  • i update my post.. i am doing wrong in the 5 line code only.Because when i add all section in one array .it showing 0 0 0 0..please any wrong with combine all NSDictionary section to one array and to add with arrayPDFName – user5513630 Nov 03 '15 at 11:36
  • actually ...in my blue section i have 37 files. in remaining i don't have data.But now while run ,in each 4 section 4 data are showing.....in my console for `NSLog(@"%i", arrayPDFName.count)` it showing `4 4 4 4` – user5513630 Nov 03 '15 at 11:48
  • @user5513630 So you should now have 4 items showing in each section, correct? – Matthew Hallatt Nov 03 '15 at 11:55
  • yes, But actually i am having 37 files in one section. in remaining i don't have data... – user5513630 Nov 03 '15 at 11:57
  • getting crash error: `'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7c4747d0'` – user5513630 Nov 03 '15 at 12:20
  • @user5513630 Ok, so maybe my syntax wasn't perfect, but the point still stands. Rather than returning the count of `arrayPDFName`, you want to use the `section` provided by `numberOfItemsInSection` to get the relevant element from `arrayPDFName` and return the number of files that has - does that make sense? – Matthew Hallatt Nov 03 '15 at 12:24