user2265763
user2265763

Reputation:

Create dynamic Sections in Uitableview from array of Unix timestamp Date ios?

I have table Events in which Dates are stored in Unix timestamp formate(i.e 1390680000) , I want to display it in uitableview with Month Wise,

I am using SQLITE3.

like,

enter image description here

aray Events:(
        {
        Id = 67;
        SDate = 1390680000;
       },
       {
        Id = 68;
        SDate = 1395600000;
       }
.
.
.

I have searched lot but i cant figure out any thing,

SO, How can i Create dynamic Section in Uitableview from Unix timestamp Date ios?

Thnks to all.

Upvotes: 0

Views: 1199

Answers (5)

Toseef Khilji
Toseef Khilji

Reputation: 17409

Correct me if i wrong,

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"];
    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];
...
}

Upvotes: 2

Ethan Fang
Ethan Fang

Reputation: 1269

This is your fetchresultController, pay attention to groupedByMonthYear

[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[DeputyCoreDataManager sharedManager].mainQueueManagedObjectContext sectionNameKeyPath:@"dttStartTime.groupedByMonthYear" cacheName:nil];

This is one of the function in your NSDate+Enhance.m category, please change it according to your date format.

- (NSString *)groupedByMonthYear{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:self];
    NSString *monthString = [NSString stringWithFormat: @"%d", [components month]];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM"];
    NSDate* myDateMonth = [dateFormatter dateFromString:monthString];

    [dateFormatter setDateFormat:@"yyyy"];
    NSString *stringFromYear = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:self]];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM"];
    NSString *stringFromMonth = [formatter stringFromDate:myDateMonth];

    return [NSString stringWithFormat:@"%@ %@", stringFromMonth, stringFromYear];

}


EDIT:

For non core data solution, it is straight forward. 1. You create a NSMutableDictionary with key as a date, and values as an array of events. 2. You loop through all your events, you get the first date of the month of that event. You then check whether there is a key in the dictionary, if it is there, you get the values, which is an array. And insert your event to that array. If the key is not there, you create an array, put the array into the map, and then add your event.

NSMutableDictionary *eventsMap = @{}.mutableCopy;
    for (Event *event in data) {
        NSDate *firstDateOfMonth = event.date.firstDateOfMonth;
        NSMutableArray *events = [eventsMap objectForKey:firstDateOfMonth];
        if (!events) {
            events = @[].mutableCopy;
            [eventsMap setObject:events forKey:firstDateOfMonth];
        }
        [events addObject:event];
    }

As for the table view

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //TODO: need sorting
        Array *sortedKeys = [eventsMap allKeys];
        return [sortedKeys objectAt:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //TODO: need sorting
        Array *sortedKeys = [eventsMap allKeys];
        return [[eventsMap objectForKey:[sortedKeys objectAt:indexPath.section]] objectAt:indexPath.row];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    Array *sortedKeys = [eventsMap allKeys];
    NSDate *titleDate = [sortedKeys objectAt:section];
    return titleDate.groupedByYearMonth;
}

Upvotes: 1

AMohan
AMohan

Reputation: 534

I think you should format the data into NSDictionary with month as a key and each key will have the message array in sorted order. So then number of sections will be

return [dictionary.allKeys count]

and you can get the number of rows for each section has is

NSArray *messages = [dictionary obejctForKey:indexPath.section]; return [messages count]

Upvotes: 1

user2071152
user2071152

Reputation: 1195

As suggested by sateesh, sample code could be as below,

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section == 0) {
           return [[dictname valueforkey:@"key1"] count];
        } else {
            return [[dictname valueforkey:@"key1"] count];
        }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
       // particular code
    } else {
      // particular code
    }
}

Upvotes: 2

sateesh
sateesh

Reputation: 497

1.use a dictionary with key as month and value as dictionary of those events comes under that month.

2.after updating the main dictionary just reload the uitableview. consider the no of sections as the main dictionary keys count and cells in every section as the sub dictionary keys count.

Upvotes: 1

Related Questions