0
votes

I am creating such table view which has not specified the number of sections (that means number of sections should be specified dynamically in its respected delegate i.e. - -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView) or not specified the rows in each sections (That means the number of rows in each sections also should be specified by dynamically in its respected delegates i.e.- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section) as below image in which the section of table specifies the month with year and each section can have any number of rows. The image is -

enter image description here

as above image january 2014 section has 4 rows and december 2013 has 2 rows. I would like to create such type of table view. Is it possible ? if it is possible than please provide proper way or any example or any link through which I can achieve it. Thanks in advanced.

2
Yes it is possible, See my answer Dynamic SecToseef Khilji
When I search in Xcode's documentation window for "UITableView", it tells me that there are 28 code samples. Are none of them useful?Phillip Mills
@Viruss mca thanks for your reply. I have seen your code it is really helpful to me. How could I display a month name with year as I have shown in above image. could you show me that standard date format which shows Month name along with year.Jekil Patel

2 Answers

3
votes

Please Use below Code. Hope It will be useful to u.

in .h file defile int i

in viewdidload method of .m file

arrData = [NSMutableArray array];

int temp = 1;

for (i = 0; i < 5; i++) {
    NSMutableArray * arrIndexData = [NSMutableArray array];
    for (int j = 0; j<=i; j++,temp++) {
        [arrIndexData addObject:[NSString stringWithFormat:@"%d",temp]];
    }
    [arrData addObject:arrIndexData];
}

Now in numberOfSectionsInTableView

return i;

in numberOfRowsInSection

return [arrData count];

in cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";

CustomCell * cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

[cell reloadTableWithArrData:[arrData objectAtIndex:indexPath.row]];

return cell;

I hope, this ll help u..

1
votes

@Jekil , Just set Your DateFormatter to MMMM yyyy ,

Check my updated Answer

As all suggests use Array of Dictionary in Dictionary put two object one for Title (Nsstring) and second is NSArray ( dict for cell detail),

I have implemented One method for that in my app,

NOTE: Use Your Key Values for your data.

Use Following code for Arranging Array,

-(NSMutableArray*)arrangeSection:(NSMutableArray *)source
{
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setLocale:[NSLocale currentLocale]];
    [_formatter setDateFormat:@"MMMM yyyy"];
    NSMutableArray *arrayMain=[NSMutableArray array];
    for (int i=0; i<source.count; i++){
        NSDictionary *dict=source[i];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[dict objectForKey:@"StartDate"]doubleValue]];
        NSString *mm=[_formatter stringFromDate:date];
        NSMutableDictionary *secDict=[NSMutableDictionary dictionary];
        NSMutableArray *secArray=[NSMutableArray array];

        if (i==0){
            [secDict setObject:mm forKey:@"Month"];
            [secArray addObject:dict];
            [secDict setObject:secArray forKey:@"Data"];
            [arrayMain addObject:secDict];
        }
        else{
            BOOL flg=NO;
            for (NSDictionary *dict2  in arrayMain){
                if([[dict2 objectForKey:@"Month"]isEqualToString:mm]){
                    flg=YES;
                    [[dict2 objectForKey:@"Data"]addObject:dict];
                    break;
                }
            }

            if (!flg){
                [secDict setObject:mm forKey:@"Month"];
                [secArray addObject:dict];
                [secDict setObject:secArray forKey:@"Data"];
                [arrayMain addObject:secDict];

            }
        }
    }
    return arrayMain;
}

Now in tableview Methods use as,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return arrayEvents.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [[arrayEvents[section]objectForKey:@"Data"]count];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

...
  NSDictionary *aDict = [arrayEvents objectAtIndex:indexPath.section];
            NSDictionary *maindict=[[aDict objectForKey:@"Data"]objectAtIndex:indexPath.row];
...
}