【问题标题】:Navigating in UITableView Slow with JSON Issue使用 JSON 问题在 UITableView 中导航缓慢
【发布时间】:2013-07-31 01:31:44
【问题描述】:

我正在工作的表格视图中加载 4 个城市字符串,但是当我选择其中一个单元格并导航到另一个表格时,它导航速度太慢。我在具有不同链接的另一个表中使用下面的相同代码。你能告诉我为什么需要很长时间(~4 - 6 秒)才能到达另一个视图吗?

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL * url  = [NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"];

NSData  * data = [NSData dataWithContentsOfURL:url];

NSError *e = nil;

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];

jsonArray1 = [[NSMutableArray alloc] init];

for(int i=0;i<[jsonArray count];i++)
{                
    NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];

    [jsonArray1 addObject:city];
}

-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

NSString *tempString = [jsonArray1 objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
return cell;
}

-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

SeconfViewController *second = [[SeconfViewController alloc] initWithNibName:@"SeconfViewController" bundle:nil];

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

NSString *cellText = selectedCell.textLabel.text;

NSString *edit = [NSString stringWithFormat:@"http://kalkatawi.com/jsonTest.php?d=1&il=%@", cellText];

second.str2 = edit;

[self.navigationController pushViewController:second animated:YES];

}

【问题讨论】:

  • 听起来你正在做的是在主线程上进行同步网络调用。查找进行异步网络调用的方法。有很多例子。

标签: iphone ios json uitableview nsjsonserialization


【解决方案1】:

在另一个屏幕上导航需要更多时间,因为您正在从服务器同步加载数据。在 iOS 中,所有 UI 都是在主线程上完成的,通过在主线程上进行数据调用,你会阻塞它。我知道处理这个问题的最好方法是使用 GCD(Grand Central Dispatch)。它是 iOS 中的一个 API,可以毫不费力地为您生成线程。您只需要告诉您要调用以在后台线程上从服务器加载数据。当你这样做时,视图应该立即导航。您可以在数据到来时使用活动指示器。

dispatch_async(dataQueue, ^{

        // Load all your data here

        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the UI

        });

    }); 

【讨论】:

    【解决方案2】:

    可能是因为您正在同步下载并且阻塞了主线程,这可能是您的应用冻结 4-6 秒的原因,请尝试异步下载您的 json

    - (void)viewDidLoad
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]];
        NSError *parseError = nil;
        jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
        jsonArray1 = [[NSMutableArray alloc] init]
            for(int i=0;i<[jsonArray count];i++)
            {                
              NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];
    
              [jsonArray1 addObject:city];
            }
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
                [self.myTableView reloadData];
            });
    });
    }
    

    【讨论】:

    • NSData 的initWithContentsOfURL: 不应用于访问remote 资源。文档对这一事实保持沉默,但在苹果官方开发者论坛上有相当多的苹果工程师声明initWithContentsOfURL: 系列方法只能用于文件访问。使用NSURLConnection 通过网络访问远程资源。
    • @CouchDeveloper @CarlosVela 现在效果更好,比以前更快。我已经提到过;首先它显示UITableView 然后加载数据是否有改善这个问题?
    • @LuaiKalkatawi 尝试使用活动指示器来指示用户正在从您的网络服务下载数据。
    猜你喜欢
    • 2012-03-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多