Ios 如何复制iPhone最近的通话通话时间标签
假设我的UITableView由X个NSDates数组提供。我如何对它们进行排序,使UITableView模仿iPhone上的手机应用程序,但在某种程度上使UITableView中的每一天都有一个部分由数组中的一个日期(或多个日期)表示 编辑:突破!为了计算我需要多少节,我使用以下方法:Ios 如何复制iPhone最近的通话通话时间标签,ios,sorting,nsdate,uitableview,Ios,Sorting,Nsdate,Uitableview,假设我的UITableView由X个NSDates数组提供。我如何对它们进行排序,使UITableView模仿iPhone上的手机应用程序,但在某种程度上使UITableView中的每一天都有一个部分由数组中的一个日期(或多个日期)表示 编辑:突破!为了计算我需要多少节,我使用以下方法: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { NSArray* array = [[NSUserDefaults
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:@"mapSaveDataKey"];
NSMutableArray* dateStrings = [[NSMutableArray alloc] init];
for(NSArray* innerArray in array)
[dateStrings addObject:[dateFormatter stringFromDate:[innerArray objectAtIndex:13]]];
NSArray *cleanedArray = [[NSSet setWithArray:dateStrings] allObjects];
return [cleanedArray count];
}
通过使用具有以下设置的NSDateFormatter获取由NSDate格式化的日期字符串数组:
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDoesRelativeDateFormatting:YES];
我可以得到一个字符串日期列表,同一天的每个字符串都是相同的,无论时间如何。然后,我可以使用NSSet获得一个包含相同字符串(不包括重复字符串)的数组,该数组的计数就是我需要的节数。太好了
现在进入困难的部分:如何获取字符串数组(或原始NSDates数组)并计算出每天有多少重复项,并使用这些信息来确定UITableView的每个部分中需要多少行?为了让事情变得更加困难,需要从新到旧进行排序。(还有一个问题是确定哪些单元格需要放在哪个部分,但这可以在以后确定)
解决了,多亏了奥勒·贝格曼雄辩的解决方案+100美元给你
我使用此循环来确定在阵列中何时需要为每个部分启动:
int offset = 0;
for(int idx = 0; idx < [tableViewOutlet numberOfSections]; idx++) {
if(idx == indexPath.section)
break;
offset += [tableViewOutlet numberOfRowsInSection:idx];
}
int offset=0;
对于(int idx=0;idx<[tableViewOutlet numberOfSections];idx++){
if(idx==indexPath.section)
打破
偏移量+=[表视图出口行数部分:idx];
}
我真的认为您应该使用核心数据和NSFetchResultController,它将为您完成所有工作-
要解释所有需要的步骤需要很长时间,但要做到:
aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:YourContext sectionNameKeyPath:@"date" cacheName:nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger count = [[[self fetchedResultsController] sections] count];
return count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows = 0;
if ([[[self fetchedResultsController] sections] count] > 0) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section];
numberOfRows = [sectionInfo numberOfObjects];
}
return numberOfRows;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[self fetchedResultsController] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[self fetchedResultsController] sectionForSectionIndexTitle:[title capitalizedString] atIndex:index];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section];
return [sectionInfo name];
}
-(NSInteger)节数表视图:(UITableView*)表视图{
NSInteger计数=[[self-fetchedResultsController]节]计数];
返回计数;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
NSInteger numberOfRows=0;
如果([[self-fetchedResultsController]节]计数]>0){
id sectionInfo=[[self-fetchedResultsController]sections]objectAtIndex:section];
numberOfRows=[sectionInfo numberOfObjects];
}
返回numberOfRows;
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)表格视图{
返回[[self-fetchedResultsController]节索引];
}
-(NSInteger)tableView:(UITableView*)分区索引的tableView部分exttitle:(NSString*)标题索引:(NSInteger)索引{
return[[self-fetchedResultsController]sectionForSectionIndexTitle:[title capitalizedString]atIndex:index];
}
-(NSString*)tableView:(UITableView*)tableView标题标题标题标题部分:(NSInteger)部分{
id sectionInfo=[[self-fetchedResultsController]sections]objectAtIndex:section];
返回[sectionInfo name];
}
希望对我个人有所帮助,我不会使用日期格式化程序。如果有更多的“数字”值(日期),处理字符串会感觉有些“脏”。使用
NSDateComponents
,从NSDate
的时间组件中分割日期同样容易。下面的示例代码相当长,但应该很容易理解。给定一个(示例)NSDate
对象数组(在dates
变量中),它将生成一个节
字典
字典的键是代表某一天的NSDate
实例(它们的时间成分是所用日历时区中的mignight)。字典的值是数组,包含属于某个部分(已排序)的实际日期。您应该能够用该信息填充表视图
// Sample data: an array of dates
NSArray *dates = [NSArray arrayWithObjects:
[NSDate dateWithTimeIntervalSince1970:1322756697],
[NSDate dateWithTimeIntervalSince1970:1322727896],
[NSDate dateWithTimeIntervalSince1970:1322699096],
[NSDate dateWithTimeIntervalSince1970:1322695496],
[NSDate dateWithTimeIntervalSince1970:1322677496],
[NSDate dateWithTimeIntervalSince1970:1322504696],
nil];
// First we sort the dates
NSArray *sortedDates = [dates sortedArrayUsingSelector:@selector(compare:)];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *utc = [NSTimeZone timeZoneForSecondsFromGMT:0];
[calendar setTimeZone:utc];
// Populate the sections dictionary
NSMutableDictionary *sections = [NSMutableDictionary dictionary];
for (NSDate *date in sortedDates) {
NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
NSDate *dateRepresentingThisDay = [calendar dateFromComponents:dateComps];
NSMutableArray *datesOnThisDay = [sections objectForKey:dateRepresentingThisDay];
if (datesOnThisDay == nil) {
// This day is new.
// Create an empty array and add it to the dictionary under this day as key.
datesOnThisDay = [NSMutableArray array];
[sections setObject:datesOnThisDay forKey:dateRepresentingThisDay];
}
[datesOnThisDay addObject:date];
}
// Output the result
NSLog(@"Found %u sections.", [sections count]);
NSArray *sortedDays = [[sections allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (NSDate *day in sortedDays) {
NSLog(@"Section: %@", day);
NSArray *datesOnThisDay = [sections objectForKey:day];
for (NSDate *date in datesOnThisDay) {
NSLog(@"-- Entry: %@", date);
}
}
编辑:我把这个问题当作是写一篇文章的机会。现在已经太晚了,我不想进入这个话题。但首先,请看一下文档。用秒计算日期是不可能的。一旦你能够开始将数组分块成日期,你就会发现处理起来更容易。谢谢你的链接,它帮了你很多忙!请参阅我的编辑…+1,了解如何处理表示脏日期的字符串。我完全明白你的意思(有1001个这样的问题,人们这样做是错的)。这也是字典的一个很好的用途。如果稍作调整,我已经实现了一半,到目前为止,它工作得非常好(为一个小节提供了正确的行数和正确的小节标题)但是现在我必须为英语考试而学习,所以我必须在明天或周末完成另一半。伟大的解决方案!哇-漂亮的实现。一次又一次地工作。谢谢你!我在这个问题上加了一点,使它起作用。