在我的表视图中我将sqlite数据库中的联系人转换为NSmutableArray * (sort By Date)
*并将填充到tableview中
所以最近添加的联系人总是被添加到表格顶部,很好。
现在我的要求是设置章节标题..使用时间间隔如下,
1 Hour Ago, 1 Day ago , 1 week ago, 1 Month ago, 1 Year ago
就是这样。
我使用逻辑来获取标题字符串,如下所示,
- (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;
}
现在我要在视图加载中获取标题(日期索引),并按如下方式设置标题标题。
// 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];
}
现在标题设置完美,但两个部分都重复了联系人。联系人没有按日期分隔到各自的部分,
我需要谓词联系到split them in their respective sections using NSprecate like following example.
仅用于示例它将所有联系人按其首字母分成各自的部分
//---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];
现在我应该如何预测我的日期间隔..
(1小时前1天前1周前)相应的细胞