UINavigationController + UITabBarController +带有TableView / MapView的UISegmentedControl

时间:2011-12-24 14:14:29

标签: objective-c ios uitableview

我正在做一些关于flickr照片处理的培训..我想展示最受欢迎的地方和相关的照片。目前,应用程序有一个TabBarController,每个选项卡都有一个导航控制器作为“起点”。故事板是:[TableView of places] - > [表格视图] - > [图像的滚动视图] 现在,我想让用户有机会切换到地图表示而不是表格视图,而不做任何进一步的segue。

我认为最好的方法是使用UISegmentedControl,所以我使用了这个布局[link to image]。当表格可见时,地图将被隐藏(反之亦然)。

仅使用表格表示,我使用了TableViewController。通过这些修改,我将UIViewController子类化,我实现了“手动”两个表协议(delegatedatasource),我链接了出口,但现在表滚动的帧率低

我问flickr最受欢迎的地方,我拆分了JSON,我获得了NSD阵列的NSD阵列,我称之为“地方”(每个字典=地方)。然后我按国家/地区分割这些地方:国家/地区列表(按字母顺序排列)称为“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; }

如果我不对一个国家/地区的地方进行排序,那么表格滚动就可以了......所以我会尝试在数据加载时开始执行排序,而不是“在运行中”滚动。

1 个答案:

答案 0 :(得分:0)

通过你给我们的小信息,我认为使你的桌子变慢的代码是你的细胞,你在细胞中展示某种形象吗?如果是这样,你是同步下载它们吗?你在网格内插入网页浏览量或大文字视图吗?

这种可以使tableview运行缓慢的事情,请添加更多信息以帮助您!