View不填充数据AFNetworking

时间:2014-03-11 07:07:21

标签: ios iphone afnetworking

我正在使用AFNetworking,我的成功块工作正常,但我的视图始终首先加载。我没有在我的视图中使用表视图,只有标签和图像需要刷新。我现在该怎么办?

-(void)myVenusWithClubDetail :(NSString *)IDs completionHandler:(void (^)(id array))success
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenuDetail];
       NSLog(@"%@",IDs);
        NSDictionary *params = @{@"Id" : IDs};
         NSLog(@"%@",link);
        [manager POST:link parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSMutableArray *dataArray = [[NSMutableArray alloc] init];

        ClubDetailDC *clubDC = [[ClubDetailDC alloc] init];
        clubDC.cDVenuID = [[responseObject objectForKey:@"Id"]integerValue];
        clubDC.cDCatID = [[responseObject objectForKey:@"cat_id"]integerValue];
        clubDC.cDName = [responseObject objectForKey:@"name"];
        clubDC.cDHeadLine = [responseObject objectForKey:@"headline"];
        clubDC.cDImage = [responseObject objectForKey:@"image"];
        clubDC.cDLong = [responseObject objectForKey:@"long"];
        clubDC.cDLat = [responseObject objectForKey:@"lat"];
        clubDC.cDAddress = [responseObject objectForKey:@"address"];
        clubDC.cDSummary = [responseObject objectForKey:@"summary"];
        clubDC.cDStat = [responseObject objectForKey:@"stat"];
        clubDC.cDUS = [responseObject objectForKey:@"us"];
        clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];

        [dataArray addObject:clubDC];
        success(dataArray);
        if (dataArray.count == 0)
        {
            ALERT_VIEW(@"Please check your internet connection.");
        }

    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             ALERT_VIEW(@"Error while loading data.")
         }];
}

我的venuVC.m就像这样我想要显示数据

-(void)reloadView
{
    clubDetail = [[ClubDetailDC alloc] init];
    venu_dataArray = [[NSMutableArray alloc] init];
    upcomingArray = [[NSMutableArray alloc] init];
    GET_DBHANDLER
    //    venu_dataArray = [dbHandler get_club_detail:venuID];
    NSString *strVenuID = [NSString stringWithFormat:@"%d",venuID];
    NSLog(@"%@",strVenuID);
    [dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
     {
         venu_dataArray = array;
         //         NSLog(@"%d",array.count);
     }];

}
-(void)viewWillAppear:(BOOL)animated
{
    [self reloadView];

    for (clubDetail in venu_dataArray)
    {
        lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
        [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
    }
    UIFont* font_name = [UIFont fontWithName:@"Corbel" size:17];
    UIFont* bold_font = [UIFont fontWithName:@"Corbel-Bold" size:17];
    lbl_upcomingEvents.font = font_name;
    lbl_Unighted.font = bold_font;
    lblDescription.font = [UIFont fontWithName:@"Corbel" size:11];
    lblCheckIn.font = [UIFont fontWithName:@"Corbel" size:13];
    lblCount.font = [UIFont fontWithName:@"Corbel" size:12];
    lblGoodEmotion.font = [UIFont fontWithName:@"Corbel" size:14];
    lblHotEmotion.font = [UIFont fontWithName:@"Corbel" size:14];
}

我的问题是首先加载视图,然后从成功块中获取数据。有人可以帮忙吗?

感谢。

4 个答案:

答案 0 :(得分:1)

由于venue_dataArray方法中reloadView是本地的,因此您无法在viewWillAppear中对其进行迭代。 所以我想,你应该将你的标签分配给成功块,如下所示:

[dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
 {
     venu_dataArray = array;
     //         NSLog(@"%d",array.count);
     for (clubDetail in venu_dataArray)
     {
        lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
        [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
     }
 }];

答案 1 :(得分:1)

您可以通过两种方式执行此操作,或者在视图中编写函数,代码将是这样的

-(void)myVenusWithClubDetail :(NSString *)IDs completionHandler:(void (^)(id array))success
{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenuDetail];
    NSLog(@"%@",IDs);
    NSDictionary *params = @{@"Id" : IDs};
    NSLog(@"%@",link);

    [manager POST:link parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);


        NSMutableArray *dataArray = [[NSMutableArray alloc] init];

        ClubDetailDC *clubDC = [[ClubDetailDC alloc] init];
        clubDC.cDVenuID = [[responseObject objectForKey:@"Id"]integerValue];
        clubDC.cDCatID = [[responseObject objectForKey:@"cat_id"]integerValue];
        clubDC.cDName = [responseObject objectForKey:@"name"];
        clubDC.cDHeadLine = [responseObject objectForKey:@"headline"];
        clubDC.cDImage = [responseObject objectForKey:@"image"];
        clubDC.cDLong = [responseObject objectForKey:@"long"];
        clubDC.cDLat = [responseObject objectForKey:@"lat"];
        clubDC.cDAddress = [responseObject objectForKey:@"address"];
        clubDC.cDSummary = [responseObject objectForKey:@"summary"];
        clubDC.cDStat = [responseObject objectForKey:@"stat"];
        clubDC.cDUS = [responseObject objectForKey:@"us"];
        clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];
        for (clubDetail in venu_dataArray)
        {
            lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
            [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
        }



        [dataArray addObject:clubDC];
        success(dataArray);

        if (dataArray.count == 0)
        {
            ALERT_VIEW(@"Please check your internet connection.");
        }

    }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
              ALERT_VIEW(@"Error while loading data.")

          }];


}

或者如果你想在separete类中使用你的函数,那么在你的成功块

中这样做
[dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
 {
     venu_dataArray = array;
     //         NSLog(@"%d",array.count);
     for (clubDetail in venu_dataArray)
     {
        lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
        [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
     }
 }];

答案 2 :(得分:0)

您也可以发布您的成功功能吗?如果您在uiimageview上的标签和图像上设置文本,则应相应更新所有内容。

您的成功功能可能如下所示:

- (void)updateViewsWithData:(NSMutableArray *)dataArray {
    var clubDC = dataArray[0];
    self.yourLabel.text = clubDC.cDSummary;
    self.addressLabel.text = clubDC.cDAddress;
    self.imageView.image = clubDC.cDImage;
}

您必须先使用AFNetworking库或类似的东西来下载图像,因为我非常确定您所获得的是图像的网址。

如果您有任何疑问,请发表评论。

答案 3 :(得分:0)

在您的代码中,

clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];

此行将根据您的实现方式从服务器或本地目录中获取图像。获得图像会有预期的延迟。

self.imageView.image = clubDC.cDImage;

执行上述代码时,图像可能无法完全下载。