Ich habe einen Code, aber ich kann nicht sehen, wie ich es optimieren kann. Obwohl ich weiß, dass der Code schlecht geschrieben ist (von mir selbst).viewDidLoad - Code Optimierung
Ich habe eine Liste der Bahnhofsnamen. Ich möchte diese Stationsnamen zu einer Tabellenansicht hinzufügen und einen Abschnitt für den ersten Buchstaben jedes Stationsnamens haben. Diejenigen Abschnitte, die keine Stationen haben, sollten übersprungen werden.
Der erste Abschnitt sollte "-" heißen und sollte die nächstgelegene Station anzeigen.
Beispiel:
--A--
Astation C
Alaska C
Alabama C
--C--
Cstation
Cathedral station
Central station
Also habe ich das alles funktioniert, aber ich fühle mich wie ich zu viel tue, sollte es ein einfacher Weg sein. Außerdem hängt die App für 1 Sekunde, wenn diese Ansicht auf mein iPhone 4 geladen wird. Das ist auch nicht gut. Dies ist mein Code:
- (void)viewDidLoad{
self.filteredListContent = [NSMutableArray array];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchBar:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];
UISearchBar *mySearchBar = [[UISearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar sizeToFit];
theTable.tableHeaderView = mySearchBar;
if (UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] ||
UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation])
{
theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f);
}
else
{
theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 320.f, 44.f);
}
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
[self setSearchDisplayController:searchDisplayController];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
[mySearchBar release];
/* Set the data */
NSArray *stationenPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *stationenDocumentsDirectory = [stationenPath objectAtIndex:0];
NSString *path = [stationenDocumentsDirectory stringByAppendingPathComponent:@"stations.plist"];
NSDictionary* stationenDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease];
xmlStationenList* stationsXml = [[xmlStationenList alloc] initWithDictionary:stationenDictionary];
NSSortDescriptor *stationSorter = [[NSSortDescriptor alloc] initWithKey:@"_station" ascending:YES];
NSMutableArray *xmlResult = [stationsXml getTimeResult];
NSArray *objects = [[[NSArray alloc] initWithObjects:stationSorter,nil] autorelease];
[xmlResult sortUsingDescriptors:objects];
[stationSorter release];
/* prefName is decided by the page opening chooseStationViewController. Using this class init function */
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.prefValue = [prefs valueForKey:prefName];
self.listContent = [NSMutableArray array];
for (stationenListSet *theList in xmlResult)
{
[self.listContent addObject:[theList get_station]];
}
/* END set the data */
/* Set sections */
self.sectionArray = [NSMutableArray array];
[self.sectionArray addObject:@"-"];
[self.sectionArray addObject:@"A"];
[self.sectionArray addObject:@"B"];
[self.sectionArray addObject:@"C"];
[self.sectionArray addObject:@"D"];
[self.sectionArray addObject:@"E"];
[self.sectionArray addObject:@"F"];
[self.sectionArray addObject:@"G"];
[self.sectionArray addObject:@"H"];
[self.sectionArray addObject:@"I"];
[self.sectionArray addObject:@"J"];
[self.sectionArray addObject:@"K"];
[self.sectionArray addObject:@"L"];
[self.sectionArray addObject:@"M"];
[self.sectionArray addObject:@"N"];
[self.sectionArray addObject:@"O"];
[self.sectionArray addObject:@"P"];
[self.sectionArray addObject:@"Q"];
[self.sectionArray addObject:@"R"];
[self.sectionArray addObject:@"S"];
[self.sectionArray addObject:@"T"];
[self.sectionArray addObject:@"U"];
[self.sectionArray addObject:@"V"];
[self.sectionArray addObject:@"W"];
[self.sectionArray addObject:@"X"];
[self.sectionArray addObject:@"Y"];
[self.sectionArray addObject:@"Z"];
[self.sectionArray addObject:@"Å"];
[self.sectionArray addObject:@"Ä"];
[self.sectionArray addObject:@"Ö"];
[sectionSubArray release];
sectionSubArray = [[NSMutableArray alloc] init];
NSMutableArray *sectionTempArray = [[NSMutableArray alloc] init];
[sectionTempArray addObject:@"-"];
[sectionSubArray addObject:[[NSArray alloc] init]];
for(NSString *sectionChar in self.sectionArray)
{
sectionChar = [sectionChar uppercaseString];
int i = 0;
NSMutableArray *sectionRowArray = [[NSMutableArray alloc] init];
for (NSString* theStation in listContent)
{
NSString *firstChar = [[NSString alloc] initWithFormat:@"%C", toupper([theStation characterAtIndex:0])];
if([firstChar isEqualToString:sectionChar])
{
[sectionRowArray addObject:theStation];
i++;
}
[firstChar release];
}
if(i > 0)
{
[sectionSubArray addObject:sectionRowArray];
[sectionTempArray addObject:sectionChar];
}
[sectionRowArray release];
}
self.sectionArray = sectionTempArray;
[sectionTempArray release];
[theTable reloadData];
theTable.scrollEnabled = YES;
[stationsXml release];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
Die dumme Sache, die ich tue, ist, dass ich Schleife durch meine Anordnung von mehr als 300 Objekten für jeden Brief, den ich habe. Aber ich kann keinen besseren Weg finden, dies zu tun.
Mit freundlichen Grüßen,
Paul Peelen
-------- -------- DAS ERGEBNIS
Dies ist, was das Ergebnis wurde dank all Ihre Antworten:
- (void)viewDidLoad{
self.filteredListContent = [NSMutableArray array];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchBar:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];
UISearchBar *mySearchBar = [[UISearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar sizeToFit];
theTable.tableHeaderView = mySearchBar;
if (UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] ||
UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation])
{
theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f);
}
else
{
theTable.tableHeaderView.frame = CGRectMake(0.f, 0.f, 320.f, 44.f);
}
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
[self setSearchDisplayController:searchDisplayController];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
[mySearchBar release];
/* Set the data */
NSArray *stationenPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *stationenDocumentsDirectory = [stationenPath objectAtIndex:0];
NSString *path = [stationenDocumentsDirectory stringByAppendingPathComponent:@"stations.plist"];
NSDictionary* stationenDictionary = [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease];
xmlStationenList* stationsXml = [[xmlStationenList alloc] initWithDictionary:stationenDictionary];
NSSortDescriptor *stationSorter = [[NSSortDescriptor alloc] initWithKey:@"_station" ascending:YES];
NSMutableArray *xmlResult = [stationsXml getTimeResult];
NSArray *objects = [[[NSArray alloc] initWithObjects:stationSorter,nil] autorelease];
[xmlResult sortUsingDescriptors:objects];
[stationSorter release];
/* prefName is decided by the page opening chooseStationViewController. Using this class init function */
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.prefValue = [prefs valueForKey:prefName];
self.listContent = [NSMutableArray array];
for (stationenListSet *theList in xmlResult)
{
[self.listContent addObject:[theList get_station]];
}
/* END set the data */
/* Set sections */
self.sectionArray = [[NSArray alloc] initWithObjects:@"-", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"Å", @"Ä", @"Ö", nil];
NSMutableDictionary *sectionDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[[NSMutableArray alloc] init], @"-",
[[NSMutableArray alloc] init], @"A",
[[NSMutableArray alloc] init], @"B",
[[NSMutableArray alloc] init], @"C",
[[NSMutableArray alloc] init], @"D",
[[NSMutableArray alloc] init], @"E",
[[NSMutableArray alloc] init], @"F",
[[NSMutableArray alloc] init], @"G",
[[NSMutableArray alloc] init], @"H",
[[NSMutableArray alloc] init], @"I",
[[NSMutableArray alloc] init], @"J",
[[NSMutableArray alloc] init], @"K",
[[NSMutableArray alloc] init], @"L",
[[NSMutableArray alloc] init], @"M",
[[NSMutableArray alloc] init], @"N",
[[NSMutableArray alloc] init], @"O",
[[NSMutableArray alloc] init], @"P",
[[NSMutableArray alloc] init], @"Q",
[[NSMutableArray alloc] init], @"R",
[[NSMutableArray alloc] init], @"S",
[[NSMutableArray alloc] init], @"T",
[[NSMutableArray alloc] init], @"U",
[[NSMutableArray alloc] init], @"V",
[[NSMutableArray alloc] init], @"W",
[[NSMutableArray alloc] init], @"X",
[[NSMutableArray alloc] init], @"Y",
[[NSMutableArray alloc] init], @"Z",
[[NSMutableArray alloc] init], @"Å",
[[NSMutableArray alloc] init], @"Ä",
[[NSMutableArray alloc] init], @"Ö", nil];
[sectionSubArray release];
sectionSubArray = [[NSMutableArray alloc] init];
for(NSString *theStation in listContent) {
NSString *firstChar = [[[NSString alloc] initWithFormat:@"%C", toupper([theStation characterAtIndex:0])] uppercaseString];
[[sectionDictionary objectForKey:firstChar] addObject:theStation];
}
NSArray *keys = [sectionDictionary allKeys];
keys = [keys sortedArrayUsingSelector:@selector(localizedCompare:)];
int indexPath = 0;
NSMutableArray *sectionTempArray = [[NSMutableArray alloc] init];
[sectionTempArray addObject:@"-"];
[sectionSubArray addObject:[[NSArray alloc] init]];
for(NSString *key in keys)
{
if ([[sectionDictionary objectForKey:key] count] > 0)
{
NSArray *subArray = [[sectionDictionary objectForKey:key] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[sectionTempArray addObject:key];
[sectionSubArray addObject:subArray];
}
indexPath++;
}
self.sectionArray = sectionTempArray;
[sectionTempArray release];
[theTable reloadData];
theTable.scrollEnabled = YES;
[stationsXml release];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
Schleife über die Stationen und füge sie dem entsprechenden Briefkorb hinzu. Sollte 26 mal schneller sein. – mvds
Ich fragte dies als Antwort auf eine Antwort, aber nur für den Fall: Was ist mit der C-style Array-Syntax? Funktioniert das sogar auf NSArrays? Warum verwenden Sie nicht die Objective-C-Methoden der Klasse wie 'NSMutableArray * sectionRowArrays = [NSMutableArray initWithCapacity: 29]' und '[[sectionRowArrays objectAtIndex: index] addObject: theStation]' usw.? –