异步请求到数据,但数据存储不到类变量里

iOS 码拜 10年前 (2015-05-11) 1144次浏览 0个评论
 
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@"rs"];
                                self.Recommendation=[weatherInfo objectForKey:@"product"];
                           }];

在viewDidLoad中log出来Recommendation却是空的。求解释。

10分
如果要在block中赋值,你要在定义那里,在最前面加上__block声明,
断点调试了下,是先进的viewDidLoad()方法,而我这个请求服务器的方法是最后调的,我现在要把获取到的数据显示在tableView中,我该怎么修改,求指点。
10分
在回调的方法里更改数据源,刷新tableview
[NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@”rs”];
                                self.Recommendation=[weatherInfo objectForKey:@”product”];

//异步请求,请求操作在新的线程中执行,执行完了会回到主线程中的这个block,所以nslog应该写在这儿
NSLog(@”%@”,self.Recommendation);
                           }];

引用 4 楼 qq_27455439 的回复:

我是在请求数据成功后,reloadData的,现在的问题是在tableView的回调方法cellForRowAtIndexPath中赋值,却没有显示。

int section = indexPath.section;
    int row = indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
        switch (section) {
            case 0:
                if(row==0)
                {
                    UIImageView *imgview =[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 80, 70)];
                    imgview.backgroundColor = [UIColor redColor];
                    NSString *imgurl= @"http://app.hfcn.cc/";
                    NSString *imgname= [_Detail objectForKey:@"thumb"];
                    NSString *imgstr = [imgurl stringByAppendingString:imgname];
                    [imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imgstr]]];
                    [cell addSubview:imgview];
                    
                    
                    UILabel *namelab= [[UILabel alloc]initWithFrame:CGRectMake(110, 5, 180, 70)];
                    namelab.font= [UIFont boldSystemFontOfSize:14.0];
                    namelab.text =  [_Detail objectForKey:@"name"];
                    [cell addSubview:namelab];
                    
                }else if(row==1)
                {
                    UIImageView *vipimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                    vipimg.backgroundColor = [UIColor redColor];
                    [cell addSubview:vipimg];
                    
                    
                    UILabel *vipcard= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                    vipcard.font= [UIFont boldSystemFontOfSize:14.0];
                    vipcard.text = @"会员卡";
                    [cell addSubview:vipcard];
                }else if(row==2)
                {
                    UIImageView *Addressimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                    Addressimg.backgroundColor = [UIColor redColor];
                    [cell addSubview:Addressimg];
                    
                    UILabel *vipcard= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                    vipcard.font= [UIFont boldSystemFontOfSize:14.0];
                    vipcard.text = @"省体育场西门";
                    [cell addSubview:vipcard];
                    
                }else
                {
                    UIImageView *telimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                    telimg.backgroundColor = [UIColor redColor];
                    [cell addSubview:telimg];
                    
                    UILabel *tellabel= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                    tellabel.font= [UIFont boldSystemFontOfSize:14.0];
                    
                    tellabel.text = [_Detail objectForKey:@"owner"];
                    [cell addSubview:tellabel];
                }
                    
                break;
                case 1:
                if(row==0)
                {
                    UILabel *Recommendation= [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 180, 25)];
                    Recommendation.font= [UIFont boldSystemFontOfSize:14.0];
                    Recommendation.text = @"推荐商品";
                    [cell addSubview:Recommendation];
                }else
                {
                   
                }
                break;
                case 2:
                if(row==0)
                {
                    UILabel *Neighborhood= [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 180, 25)];
                    Neighborhood.font= [UIFont boldSystemFontOfSize:14.0];
                    Neighborhood.text = @"附近商家";
                    [cell addSubview:Neighborhood];
                }else
                {
                 
                }
            default:
                break;
        }
    }
    return cell;
}
  [NSURLConnection sendAsynchronousRequest:request
                                      queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@"rs"];
      _tableview.reloadData;
                           }];

引用 6 楼 ningshuang520 的回复:
  [NSURLConnection sendAsynchronousRequest:request
                                      queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@"rs"];
_Detail = ?????
      _tableview.reloadData;
                           }];

你的cell只用到了”_Detail”这一个数据源,你得在reloadData之前更新这个_Detail;

引用 7 楼 qq_27455439 的回复:

 [NSURLConnection sendAsynchronousRequest:request
                                      queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@”rs”];
                               
                               _Collect= [weatherInfo objectForKey:@”favorites”];
                               NSLog(@”favorites_===%@”,_Collect);
             
                               [_tableview reloadData];
                           }];

引用 7 楼 qq_27455439 的回复:
Quote: 引用 6 楼 ningshuang520 的回复:
  [NSURLConnection sendAsynchronousRequest:request
                                      queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@"rs"];
_Detail = ?????
      _tableview.reloadData;
                           }];

你的cell只用到了”_Detail”这一个数据源,你得在reloadData之前更新这个_Detail;

http://pan.baidu.com/s/1jGEIbPW
你看我这个代码。

10分
引用 8 楼 ningshuang520 的回复:
Quote: 引用 7 楼 qq_27455439 的回复:

 [NSURLConnection sendAsynchronousRequest:request
                                      queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@”rs”];
                               
                               _Collect= [weatherInfo objectForKey:@”favorites”];
                               NSLog(@”favorites_===%@”,_Collect);
             
                               [_tableview reloadData];
                           }];

_Collect 打印出来是不是有值的?如果是的话,在 numberOfRows 那里再打断点,再次确认下数据源,最后才在 cellForRow 这里跟踪。

引用 10 楼 zhangao0086 的回复:
Quote: 引用 8 楼 ningshuang520 的回复:
Quote: 引用 7 楼 qq_27455439 的回复:

 [NSURLConnection sendAsynchronousRequest:request
                                      queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *received,NSError *error) {
                               //json解析
                               NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error];
                               NSDictionary *weatherInfo = [weatherDic objectForKey:@”rs”];
                               
                               _Collect= [weatherInfo objectForKey:@”favorites”];
                               NSLog(@”favorites_===%@”,_Collect);
             
                               [_tableview reloadData];
                           }];

_Collect 打印出来是不是有值的?如果是的话,在 numberOfRows 那里再打断点,再次确认下数据源,最后才在 cellForRow 这里跟踪。

请求之后_Collect获取到值了。你看我这个demo代码。
http://pan.baidu.com/s/1jGEIbPW

引用 5 楼 ningshuang520 的回复:
Quote: 引用 4 楼 qq_27455439 的回复:

我是在请求数据成功后,reloadData的,现在的问题是在tableView的回调方法cellForRowAtIndexPath中赋值,却没有显示。

int section = indexPath.section;
    int row = indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
        switch (section) {
            case 0:
                if(row==0)
                {
                    UIImageView *imgview =[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 80, 70)];
                    imgview.backgroundColor = [UIColor redColor];
                    NSString *imgurl= @"http://app.hfcn.cc/";
                    NSString *imgname= [_Detail objectForKey:@"thumb"];
                    NSString *imgstr = [imgurl stringByAppendingString:imgname];
                    [imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imgstr]]];
                    [cell addSubview:imgview];
                    
                    
                    UILabel *namelab= [[UILabel alloc]initWithFrame:CGRectMake(110, 5, 180, 70)];
                    namelab.font= [UIFont boldSystemFontOfSize:14.0];
                    namelab.text =  [_Detail objectForKey:@"name"];
                    [cell addSubview:namelab];
                    
                }else if(row==1)
                {
                    UIImageView *vipimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                    vipimg.backgroundColor = [UIColor redColor];
                    [cell addSubview:vipimg];
                    
                    
                    UILabel *vipcard= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                    vipcard.font= [UIFont boldSystemFontOfSize:14.0];
                    vipcard.text = @"会员卡";
                    [cell addSubview:vipcard];
                }else if(row==2)
                {
                    UIImageView *Addressimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                    Addressimg.backgroundColor = [UIColor redColor];
                    [cell addSubview:Addressimg];
                    
                    UILabel *vipcard= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                    vipcard.font= [UIFont boldSystemFontOfSize:14.0];
                    vipcard.text = @"省体育场西门";
                    [cell addSubview:vipcard];
                    
                }else
                {
                    UIImageView *telimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                    telimg.backgroundColor = [UIColor redColor];
                    [cell addSubview:telimg];
                    
                    UILabel *tellabel= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                    tellabel.font= [UIFont boldSystemFontOfSize:14.0];
                    
                    tellabel.text = [_Detail objectForKey:@"owner"];
                    [cell addSubview:tellabel];
                }
                    
                break;
                case 1:
                if(row==0)
                {
                    UILabel *Recommendation= [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 180, 25)];
                    Recommendation.font= [UIFont boldSystemFontOfSize:14.0];
                    Recommendation.text = @"推荐商品";
                    [cell addSubview:Recommendation];
                }else
                {
                   
                }
                break;
                case 2:
                if(row==0)
                {
                    UILabel *Neighborhood= [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 180, 25)];
                    Neighborhood.font= [UIFont boldSystemFontOfSize:14.0];
                    Neighborhood.text = @"附近商家";
                    [cell addSubview:Neighborhood];
                }else
                {
                 
                }
            default:
                break;
        }
    }
    return cell;
}

把switch 这一段放到if(cell == nil){  …  }外面就可以了。。。

确实,那个 switch 的地方不对
看了下代码,13楼正解 switch放if外面
//每行内容
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@”详情========%@”,_Detail);
    int section = indexPath.section;
    int row = indexPath.row;
    static NSString *CellIdentifier = @”Cell”;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
    }

    switch (section) {
        case 0:
            if(row==0)
            {
                UIImageView *imgview =[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 80, 70)];
                imgview.backgroundColor = [UIColor redColor];
                //                    NSString *imgurl= @”http://app.hfcn.cc/”;
                //                    NSString *imgname= [_Detail objectForKey:@”thumb”];
                //                    NSString *imgstr = [imgurl stringByAppendingString:imgname];
                //                    [imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@”%@”,imgstr]]];
                [cell addSubview:imgview];
                
                
                UILabel *namelab= [[UILabel alloc]initWithFrame:CGRectMake(110, 5, 180, 70)];
                namelab.font= [UIFont boldSystemFontOfSize:14.0];
                namelab.text =  [_Detail objectForKey:@”name”];
                [cell addSubview:namelab];
                
            }else if(row==1)
            {
                UIImageView *vipimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                vipimg.backgroundColor = [UIColor redColor];
                [cell addSubview:vipimg];
                
                
                UILabel *vipcard= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                vipcard.font= [UIFont boldSystemFontOfSize:14.0];
                vipcard.text = @”会员卡”;
                [cell addSubview:vipcard];
            }else if(row==2)
            {
                UIImageView *Addressimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                Addressimg.backgroundColor = [UIColor redColor];
                [cell addSubview:Addressimg];
                
                UILabel *vipcard= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                vipcard.font= [UIFont boldSystemFontOfSize:14.0];
                //地址
                vipcard.text = [_Detail objectForKey:@”address”];
                [cell addSubview:vipcard];
                
            }else
            {
                UIImageView *telimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 28, 28)];
                telimg.backgroundColor = [UIColor redColor];
                [cell addSubview:telimg];
                //电话
                UILabel *tellabel= [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
                tellabel.font= [UIFont boldSystemFontOfSize:14.0];
                tellabel.text = [_Detail objectForKey:@”owner”];
                [cell addSubview:tellabel];
            }
            
            break;
        case 1:
            if(row==0)
            {
                UILabel *Recommendation= [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 180, 25)];
                Recommendation.font= [UIFont boldSystemFontOfSize:14.0];
                Recommendation.text = @”推荐商品”;
                [cell addSubview:Recommendation];
            }else
            {
                
            }
            break;
        case 2:
            if(row==0)
            {
                UILabel *Neighborhood= [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 180, 25)];
                Neighborhood.font= [UIFont boldSystemFontOfSize:14.0];
                Neighborhood.text = @”附近商家”;
                [cell addSubview:Neighborhood];
            }else
            {
                
            }
            break;
        default:
            break;
    }

    return cell;
}

引用 15 楼 qq_27455439 的回复:

看了下代码,13楼正解 switch放if外面

这个加载图片的时候,报德这个错,求指点。异步请求到数据,但数据存储不到类变量里

引用 16 楼 ningshuang520 的回复:
Quote: 引用 15 楼 qq_27455439 的回复:

看了下代码,13楼正解 switch放if外面

这个加载图片的时候,报德这个错,求指点。异步请求到数据,但数据存储不到类变量里

看下imgname是否为空吧

引用 17 楼 qq_27455439 的回复:

这块有问题,我不知道为什么前几次出来的数据是空的,不应该啊,我是在请求服务器后reloadData的。

引用 18 楼 ningshuang520 的回复:
Quote: 引用 17 楼 qq_27455439 的回复:

这块有问题,我不知道为什么前几次出来的数据是空的,不应该啊,我是在请求服务器后reloadData的。

那就是服务器下载下来的_Detail里压根就没有@“thumb”这个Key,或者这个key本来就是空值,你可以打印下_Detail看下。
多嘴一句,你的问题很基础,公司就你一个人吗?

引用 19 楼 qq_27455439 的回复:
Quote: 引用 18 楼 ningshuang520 的回复:
Quote: 引用 17 楼 qq_27455439 的回复:

这块有问题,我不知道为什么前几次出来的数据是空的,不应该啊,我是在请求服务器后reloadData的。

那就是服务器下载下来的_Detail里压根就没有@“thumb”这个Key,或者这个key本来就是空值,你可以打印下_Detail看下。
多嘴一句,你的问题很基础,公司就你一个人吗?

恩打印出来的数据前几次是nil,后几次是由数据的。公司这边IOS确实只有我一个人,之前一个同事离职了。

10分
你用的回调方式取值,第一次绘制cell的时候_Detail不一定有值,取key之前先判断一下_Detail是否为空就可以了。
引用 20 楼 ningshuang520 的回复:

恩打印出来的数据前几次是nil,后几次是由数据的。公司这边IOS确实只有我一个人,之前一个同事离职了。

建议你每次赋值的时候都判断一下你赋的那个值是不是你想要的类型,
if( ! [string isKindOfClass:[NSNull Class] ] )//先判断是否为空
{
       //赋值。。。。。。。
}

//周末愉快


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明异步请求到数据,但数据存储不到类变量里
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!