#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
namespace win32 {
#include <windows.h>
}
class timer
{
win32::LARGE_INTEGER start_time_;
public:
timer() { QueryPerformanceCounter( &start_time_ ); }
void restart() { QueryPerformanceCounter( &start_time_ ); }
double elapsed() const
{
win32::LARGE_INTEGER end_time, frequency;
QueryPerformanceCounter( &end_time );
QueryPerformanceFrequency( &frequency );
return double( end_time.QuadPart - start_time_.QuadPart )
/ frequency.QuadPart;
}
};
#else
#include <ctime>
class timer
{
clock_t _start_time;
public:
timer() { _start_time = clock(); }
void restart() { _start_time = clock(); }
double elapsed() const
{
return double(clock() - _start_time) / CLOCKS_PER_SEC;
}
};
#endif
template< typename Func >
double measure_time( Func f )
{
timer t;
f();
return t.elapsed();
}
Вы должны использовать свой CoreData NSFetchedResultsController для получения ваших разделов / индексов.
Вы можете указать ключ раздела в запросе на выборку (я думаю, он должен соответствовать первому ключу сортировки):
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"name" // this key defines the sort
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext
sectionNameKeyPath:@"name" // this key defines the sections
cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
Затем вы можете получить имена разделов следующим образом:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
И индексы разделов здесь:
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
[sectionInfo indexTitle]; // this is the index
Изменения в содержании просто указывают на то, что таблицу необходимо обновить:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView reloadData];
}
UPDATE
Это работает только для индекса и быстрой прокрутки индекса, но не для заголовков разделов.
См. этот ответ на «Как использовать первый символ в качестве имени раздела» для получения дополнительной информации и подробностей о том, как реализовать первые буквы для заголовков разделов, а также индекса.