Мне нужно разобрать и отсортировать массив с более чем 100 записями.У меня есть метод для разбора и сортировки ниже.Подвох: мне нужно, чтобы строки, хранящиеся в NSMutableArray, отображались в tableView немедленно, когда ViewController, содержащий tableView, представлен / загруженЕсли я этого не сделаю, пользовательский интерфейс зависает / зависает при вызове ViewController.Перемещение кода в один из моих синглетонов увеличивает время загрузки, чего мне также нужно избегать.
Я думаю, мне нужно поставить в очередь / заблокировать этот код, но я не уверен, как это сделать.Любое руководство с благодарностью!
Вот код, который мне нужно проанализировать [NSTimeZone knownTimeZones]
:
-(void)parseTimeZoneNames {
for (int i = 0; i < [[NSTimeZone knownTimeZoneNames] count]; i++) {
NSString *fullName = [NSString stringWithFormat:@"%@", [[NSTimeZone knownTimeZoneNames] objectAtIndex:i]];
NSRange firstForwardSlash = [fullName rangeOfString:@"/"];
NSRange firstNameRange = NSMakeRange(firstForwardSlash.location + 1, ([fullName length] - firstForwardSlash.location - 1));
NSString *cityName = [NSString stringWithFormat:@"%@", [fullName substringWithRange:firstNameRange]];
NSRange secondforwardSlash = [cityName rangeOfString:@"/"];
if(!secondforwardSlash.length) {
NSRange secondNameRange = NSMakeRange(secondforwardSlash.location + 1, ([cityName length] - secondforwardSlash.location - 1));
cityName = [NSString stringWithFormat:@"%@", [cityName substringWithRange:secondNameRange]];
}
NSRange underscore = [cityName rangeOfString:@"_"];
if(!underscore.length) {
NSRange finalNameRange = NSMakeRange(underscore.location + 1, ([cityName length] - underscore.location - 1));
cityName = [NSString stringWithFormat:@"%@", [cityName substringWithRange:finalNameRange]];
}
[timeZonesArray addObject:cityName];
}
[timeZonesArray sortUsingSelector:@selector(compare:)];
}
Вот код для его представления в том же ViewController:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[NSTimeZone knownTimeZoneNames] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", [timeZonesArray objectAtIndex:indexPath.row]];
return cell;
}