2011-12-24 69 views
0

我正在做一些關於flickr照片處理的培訓..我想顯示最流行的地方和相關照片。目前,該應用程序有一個TabBarController,並且每個選項卡都有一個導航控制器作爲「起點」。故事板是:[TableView of places] - > [Table View of photos] - > [Scroll View of the image] 現在,我想讓用戶有機會切換到地圖表示而不是表格視圖,沒有做任何進一步的細分。UINavigationController + UITabBarController + UISegmentedControl與TableView/MapView

我認爲最好的辦法是使用UISegmentedControl,所以我用這個佈局[link to image]。當表格可見時,地圖是隱藏的(反之亦然)。

僅使用表格表示法,我使用了TableViewController。通過這個修改,我分類了一個UIViewController,並且我實現了「手工」兩個表協議(delegatedatasource),我連接了網點,但是現在表滾動的幀率很低

我問flickr最流行的地方,我拆分JSON,並獲得NSDictionary的NSArray,我稱之爲'地方'(每個字典=地方)。然後,我按國家分段分割這些地方:國家列表(按字母順序排列)被稱爲'sortedCountries'。然後,我創建了一個名爲'placesByCountry'的NSDictionary(key = country/value =該國家的地方數組)。

這裏是一些代碼,我寫道:

- (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時)。

編輯: 也許我發現問題...表滾動具有低幀率,可以看到,在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; } 

如果我沒有一個國家的地方進行排序,表滾動是好的..所以我會嘗試在剛開始的時候進行排序數據被加載,而不是在滾動時「即時」。

+0

這聽起來像是正確的方法,我不知道爲什麼你得到一個低幀率你能給我們更多的細節,你是什麼意思?我懷疑如果刷新tableview時出現問題,你會在滾動表格時做一些計算繁重的工作(可能更新隱藏地圖?) – 2011-12-24 18:18:32

+0

@ScottSherwood問題已更新;) – Ciampo 2011-12-24 20:54:46

回答

0

隨着你給我們的小信息,我認爲讓你的桌子變慢的代碼片段就是你的單元格,你是在單元格中顯示某種圖像嗎?如果是這樣,你是否同步下載它們?你在單元格內插入了webviews還是大文本?

這是一種可以使桌面運行速度變慢的事情,請添加更多信息以幫助您!

+0

問題已更新;) – Ciampo 2011-12-24 20:55:32

相關問題