2

Sorry to ask again the question with full description.What i have that i have resultsArray which has title description etc which gets from server but problem is that i want to show this data in sections.

So lets say if have three sections which are coming from array then how will populate the data in each section using single resultArray.

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
return [categoryArray objectAtIndex:section];
}

resutArray has all the data for all sections so how to show according to sections

Structure of array is

             ObjectData *theObject =[[ObjectData alloc] init];
            [theObject setCategory:[dict objectForKey:@"category"]];
            [theObject setSub_Category:[dict objectForKey:@"sub_Category"]];    
            [theObject setContent_Type:[dict objectForKey:@"content_Type"]];
            [theObject setContent_Title:[dict objectForKey:@"content_Title"]];
            [theObject setPublisher:[dict objectForKey:@"publisher"]];
            [theObject setContent_Description:[dict objectForKey:@"content_Description"]];
    [theObject setContent_ID:[dict objectForKey:@"content_ID"]];
    [theObject setContent_Source:[dict objectForKey:@"content_Source"]];
        [resultArray addObject:theObject];
10
  • will you please show the structure of your array, what data are you storing in it. and on which item you want to show in section. Commented Mar 26, 2013 at 7:07
  • @DipenPanchasara i have added the structure of the array Commented Mar 26, 2013 at 7:11
  • Show your code then easy to solve..Thansk Commented Mar 26, 2013 at 8:26
  • you can calculate number of objects in row, then go on to start from next one. Is this so difficult? Commented Mar 26, 2013 at 8:30
  • @AnoopVaidya i did not understand can you explain more Commented Mar 26, 2013 at 8:31

5 Answers 5

2

Look's like you have ObjectData which has attribute called category, and you want to show table view with ObjectData's grouped by categories. You may do this generating an NSDictionary with key = unique category, and value = NSArray of ObjectData of that category. Then you use that NSDictionary as a data for datacource.

NSArray *tempArray =[[DataController staticVersion] startParsing:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/catalogMaster.php"];
// dictionary for cells, you'll probably want this to be ivar or property
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
// array for headers, you'll probably want this to be ivar or property
NSMutableArray* categories = [NSMutableArray array];

for (int i = 0; i<[tempArray count]; i++) {

    id *item = [tempArray objectAtIndex:i];
    NSDictionary *dict = (NSDictionary *) item;
    ObjectData *theObject =[[ObjectData alloc] init];
    [theObject setCategory:[dict objectForKey:@"category"]];
    [theObject setSub_Category:[dict objectForKey:@"sub_Category"]];
    [theObject setContent_Type:[dict objectForKey:@"content_Type"]];
    [theObject setContent_Title:[dict objectForKey:@"content_Title"]];
    [theObject setPublisher:[dict objectForKey:@"publisher"]];
    [theObject setContent_Description:[dict objectForKey:@"content_Description"]];
    [theObject setContent_ID:[dict objectForKey:@"content_ID"]];
    [theObject setContent_Source:[dict objectForKey:@"content_Source"]];


    [resultArray addObject:theObject];
    [theObject release];
    theObject=nil;

    // here you populate that dictionary and categories array
    NSMutableArray* objs = [dictionary objectForKey:theObject.category];
    if(!objs)
    {
        objs = [NSMutableArray array];
        [dictionary setObject:objs forKey:theObject.category];
        [categories addObject:theObject.category];
    }
    [objs addObject:theObject];
}

Then in your controller:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [categories count] ;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 {

      return [categories objectAtIndex:section];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }

    NSString* category = [categories objectAtIndex: indexPath.section];
    NSMutableArray* objs = [dictionary objectForKey:category];
    ObjectData* obj = [objs objectAtIndex: indexPath.row];

    // populate cell here

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSString* category = [categories objectAtIndex:section];
    NSMutableArray* objs = [dictionary objectForKey:category];
    return [objs count];
}

Note that that's just an example for you, you may optimize this as you want.

Sign up to request clarification or add additional context in comments.

4 Comments

yes you are right i want like this can you please add some code if you can how to add like these
instead of categoires i think we have to give resultArray in which result is fetched from json
it is giving error index.path undeclared in numberofRowsInSection
Just a typo, fixed. I'm writing code here in web interface, so it's possible to meet some typos and small errors here.
1

Using custom class you can achieve this.

myClass.h

@interface myClass : NSObject
@property(nonatomic,retain) NSString* sectionHeader;
@property(nonatomic,retain) NSMutableArray* sectionRows;
@end

myClass.m

#import "myClass.h"

@implementation myClass
@synthesize sectionHeader;
@synthesize sectionRows;
@end

First you need to set up your resultArray with above class object. Set Appropriate value for class. Example:

myClass *myClassObj = [[myClass alloc] init];
myClassObj.sectionHeader = @"Your section title";
myClassObj.sectionRows   = < Your NSMutableArray for rows under current section >;
[retunArray addObject:myClassObj];

Now Set numberOfSectionsInTableView , numberOfRowsInSection, titleForHeaderInSection, cellForRowAtIndexPath

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [returnArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[returnArray objectAtIndex:section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    myClass *myClassObject = [returnArray objectAtIndex:section];
    return myClassObject.sectionHeader;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    myClass *myClassObject = [returnArray objectAtIndex:indexPath.section];
    NSLog(@"%@",[myClassObject.sectionRows objectAtIndex:indexPath.row]);

    return cell;
}

9 Comments

but how will it separte each sections for data for differnet title
you are right lets say i have title sections in array and the resultArray contains all the data how it will separate data for each sections from resultArray
see my resultArray as i told and categoryArray which is the sections array title
Your returnArray has all the data for all sections right. now you need to set object say of type NSMutableArray to your ObjectData class. and you can access that array and value as shown above
what to give myClassObje.sectionTitle=@"Your Section Title" this comes mine from categoryArray
|
0

If all you have is an NSMutableArray and you want to split it into sections means you have to know index of objects. For example,

[
Section 1,
Section1,
Section1,
Section 2,
Section 2,
Section2,
Section 3,
Section 3,
Section 3
]

in 1st sections display data from 0 to 2 index, in 2nd from 3 to 5 index, in 3rd from 6 to 8 index.

Else better go for NSDictionary. Keep section title as key and the values as NSArray in Dictionary. So it will be easier to load the cell.

You have NSArray Of Object Data.. So you can do it as

    NSMutableDictionary *sectionDict = [[NSMutableDictionary alloc] init];

    for(int i=0; i < [data count]; i++)
    {
ObjectData *theObject = [data objectAtIndex:i];
        NSMutableArray *arr;
        if([sectionDict valueForKey:theObject.category])
        {
            arr = [sectionDict valueForKey:theObject.category];
        }
        else
        {
            arr = [[NSMutableArray alloc] init];
        }
        [arr addObject:theObject];
        [sectionDict setObject:arr forKey:theObject.category];
    }

So now you will have required data...

3 Comments

yes section title will be best to make key and load data any e.g how to do this please
See updated answer.. You have split the data based on indexes and should have separate key for all sections...
i did not get this can you add my code as i have made more clear please
0

Do same as in the sample code

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [sectionArray count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
 //Do your stuff
} 

1 Comment

i do not know the exact number of section some time it may be 10 some time more that that or less that 10 becuase array is coming from server
0

You might also find it much easier to use the free Sensible TableView framework. The framework will simply take your array and generate all the table view cells and sections, including all the detail views if you wish. I find it much simpler and literally takes a few minutes to implement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.