1

In my table view i get the contacts from sqlite database into an NSmutableArray *(sort By Date)* and populate them into the tableview.

So recently added contact is always added to the top of the table, fine.

Now my requirement is to set Section Headers.. using time intervals as follows,

1 Hour Ago, 1 Day ago , 1 week ago, 1 Month ago, 1 Year ago like that.

I imlemented logic to get the header strings as follows,

- (NSString *)interval {
      NSDate *date = self.time; // self.time get from database for each contact insert time
      double timeInterval = [date timeIntervalSinceNow];

      NSString *tmpValue = nil;
      timeInterval *= -1;

      if(timeInterval < 3600*24) {
            tmpValue = @"Today";

      }

      else if (timeInterval > 3600*24) {
            int div = round(timeInterval / 60 / 60 / 24);
            if (div ==1 && div <7)
                  //tmpValue= [NSString stringWithFormat:@"%d days ago", div];
                  tmpValue = @"This Week";

            else if(div <30)
                  tmpValue = @"This Month";
            else 
                  tmpValue = @"Earlier";
      } 
      NSLog(@"xxxyyyyyy%@",tmpValue);

      return tmpValue;


}

Now im going to get the Headers (Date Indexes) in view load and, set the headers titles as follows.

// Implemented logic for Recent Contacts fetched from recent Ids in view will appear
      //=================================================
      recentContactsArray = [[NSMutableArray alloc] init];

      datesArray = [[NSMutableArray alloc] init];
      for(int i=0; i< storedRecentArray.count ; i++)
      {
            Recent *r = [storedRecentArray objectAtIndex:i];
            NSMutableArray *recentContact =  [DataBase selectContactsByRowId:r.recentRowId];
            Contact *recentContactDict = [recentContact objectAtIndex:0];            
            [recentContactsArray addObject: recentContactDict ];

            [datesArray addObject:[r interval]];
      }


      dateIndex = [[NSMutableArray alloc] init];
      for (int i=0; i<[datesArray count]; i++)
      {
            //---get the date wise headers of each contactName---
            NSString *dateHeader = [NSString stringWithFormat:@"%@",  [datesArray objectAtIndex:i]];
            //---add each date wise headers to the index array---
            if (![dateIndex containsObject:dateHeader])
            {            
                  [dateIndex addObject:dateHeader];
            }        
      }
      NSLog(@"Date Insex array is xxxxxxxx %@", dateIndex);

// Set the section count as number of date indexes
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
            return [dateIndex count];
}

// Set the section Header from date indexes
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

     return   [dateIndex objectAtIndex:section];   

    }

Now the headers set perfectly but contacts are repeated in both the sections.Contacts are not separated date wise into their respective sections,

I need to predicate contacts to split them in their respective sections using NSprecate like following example.

Just for Sample It splits the all the contacts by their initial letters to their respective sections

 //---get all contactNames beginning with the letter---
        NSPredicate *predicate =  [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
        NSArray *contacts = [contactsArray filteredArrayUsingPredicate:predicate];
        if (isSearchOn)
        {
              contacts = [searchedNamesArray filteredArrayUsingPredicate:predicate];
        }
        //---return the number of contactNames beginning with the letter---
        return [contacts count];   

Now how should i predicate for my date intervals..

(1 hour ago 1day ago 1 week ago) corresponding cells

Pratyusha Terli
  • 2,343
  • 19
  • 31
user1811427
  • 359
  • 2
  • 6
  • 17

0 Answers0