我有一些代碼,但我看不到如何優化它。 Allthough我知道代碼寫得很糟(我自己)。viewDidLoad - 代碼優化
我所擁有的是火車站名稱列表。我想要做的就是將這些電臺名稱添加到桌面視圖中,併爲每個電臺名稱的第一個字母設置一個部分。應該跳過那些沒有任何電臺的部分。
第一部分應該被稱爲「 - 」,並應顯示最近的車站。
例子:
--A--
Astation C
Alaska C
Alabama C
--C--
Cstation
Cathedral station
Central station
所以我得到了這一切工作,但我覺得我做的不怎麼樣,應該有一個更簡單的方法。此外,在我的iPhone 4上加載此視圖時,該應用程序會掛起1秒鐘。這也不算什麼。 這是我的代碼:
- (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];
}
我做的愚蠢的事情是,我環路經過我的300對象的每一封信我的陣列。但我找不到一個更好的方法來做到這一點。
最好的問候,
保羅Peelen
--------結果--------
這是個什麼結果變成了,感謝所有的答案:
- (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];
}
在站上循環並將它們添加到適當的字母桶中。應該快26倍。 – mvds 2011-01-11 00:30:34
我在回答這個問題時提出了這個問題,但以防萬一:C語言風格的數組語法是什麼?這甚至在NSArrays上工作嗎?爲什麼不使用類的標準Objective-C方法,比如`NSMutableArray * sectionRowArrays = [NSMutableArray initWithCapacity:29]`,和[[SectionRowArrays objectAtIndex:index] addObject:theStation]`等等? – 2011-01-11 10:45:13