flickr写真の取り扱いについていくつかトレーニングをしています。私は最も人気のある場所と関連する写真を表示したいと思います。現在のところ、アプリケーションにはTabBarControllerがあり、各タブには「開始点」としてのナビゲーションコントローラがあります。ストーリーボードは次のとおりです:[場所のTableView] - > [写真のテーブルビュー] - > [画像のスクロールビュー] 今、私はユーザーにテーブルビューではなくマップ表示に切り替える機会を与えたいと思います。それ以上のセグをせずに。UINavigationController + UITabBarController + TableView/MapViewを使用したUISegmentedControl
私はこれを行う最良の方法はUISegmentedControl
を使用することだと思いますので、このレイアウトを使用しました[link to image]。テーブルが表示されていると、マップは非表示になります(およびその逆もあります)。
テーブル表現だけを使用して、私はTableViewControllerを使用しました。この変更により、私はUIViewControllerをサブクラス化し、2つのテーブルプロトコル(delegate
とdatasource
)を "手作業で実装"しましたが、今はテーブルスクロールのフレームレートが低いです。
私はflickrに最も人気のある場所を尋ねます。私はJSONを分割し、NSDictionaryのNSArrayを取得します。これは 'places'(各辞書= place)と呼ばれています。それから、私は国ごとにセクションを分割しました。アルファベット順に並べられた国のリストは「sortedCountries」と呼ばれます。次に、 'placesByCountry'(キー= country/value =その国の場所の配列)というNSDictionaryを作成しました。
ここ私が書いたいくつかのコード:
- (NSArray*)addressForPlace:(NSDictionary*)place {
return [[place objectForKey:@"_content"] componentsSeparatedByString:@", "]; }
- (NSString *)countryForSection:(NSInteger)section {
return [self.sortedCountries objectAtIndex:section]; }
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self countryForSection:section]; }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.placesByCountry count]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *country = [self countryForSection:section];
NSArray *placesByCountry = [self.placesByCountry objectForKey:country];
return [placesByCountry count]; }
- (UITableViewCell *)tableView:(UITableView *)tableViewLocal cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Top Place Cell";
UITableViewCell *cell = [tableViewLocal dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *country = [self countryForSection:indexPath.section];
NSArray *placesByCountry = [self orderedPlacesForCountry:country];
NSDictionary *place = [placesByCountry objectAtIndex:indexPath.row];
NSArray *address = [self addressForPlace:place];
cell.textLabel.text = [address objectAtIndex:0];
NSMutableString *remainigAddres = [NSMutableString string];
for (NSString *s in address) {
if ([address indexOfObject:s] != 0) {
if ([address indexOfObject:s] != [address indexOfObject:[address lastObject]])
[remainigAddres appendFormat:@"%@, ", s];
else
[remainigAddres appendString:s];
}
}
cell.detailTextLabel.text = remainigAddres;
return cell; }
は、これが私の問題のため正しいアプローチですか? のパフォーマンスを改善するにはどうすればよいですか?私はちょうどセル識別子をチェックして、ダウンロードするイメージではありません!唯一の非同期キューは、テーブルの最初の人口で発生します(私がflickrを照会するとき)。
EDIT: おそらく私は問題を見つけました...テーブルのスクロールは、特に大きなセクションをスクロールするときにフレームレートが低くなります。 tableView:cellForRowAtIndexPath:メソッドで、私はorderedPlacesForCountry:という別のメソッドを呼び出します。 ..私は、国の場所をソートしていない場合は
- (NSArray*)orderedPlacesForCountry:(NSString*)country {
NSArray* places = [self.placesByCountry objectForKey:country];
if (DEBUG_LOG_ACTIVE) NSLog(@"places: %d", [places count]);
NSMutableArray* cities = [NSMutableArray array];
for (NSDictionary* place in places) {
[cities addObject:[[self addressForPlace:place] objectAtIndex:0]];
}
if (DEBUG_LOG_ACTIVE) NSLog(@"cities: %d", [cities count]);
NSArray *sortedCities = [cities sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
if (DEBUG_LOG_ACTIVE) NSLog(@"sorted cities: %d", [sortedCities count]);
NSMutableArray *sortedPlaces = [NSMutableArray arrayWithCapacity:[places count]];
for (NSString* city in sortedCities) {
//per ogni città cerco in places quel place
for (NSDictionary* place in places) {
if ([[[self addressForPlace:place] objectAtIndex:0] isEqualToString:city])
//e lo aggiungo a sorted
[sortedPlaces addObject:place];
}
}
if (DEBUG_LOG_ACTIVE) NSLog(@"sorted places: %d", [sortedPlaces count]);
return sortedPlaces; }
、テーブルのスクロールが結構ですので、私は最初、で並べ替えを実行してみましょう:このメソッドは、オンザフライでのソートを行い、スクロール中はデータがロードされ、「オンザフライ」ではありません。
これは正しいアプローチのように聞こえますが、低いフレームレートを取得している理由がわからないのはなぜですか?テーブルがスクロールしているときに計算上重い何かをしているテーブルビューをリフレッシュする際に問題があると思われますか? –
@ScottSherwood question; updated – Ciampo