活动指示器不在按钮上工作单击IOS

时间:2017-08-02 11:27:36

标签: ios objective-c

我有一个表单,当用户点击搜索按钮时,在选择后会有一些文本字段和下拉列表,新的tableviewcontroller会打开。在此表视图控制器中,它加载一个从服务中提取数据的函数。数据加载了一段时间,现在我想在表格视图中加载数据的时间段之间使用活动指标我已经使用了指标,但它不起作用。我的代码是,

    -(void)loadCustomerRecords

{

    _City = nil;

    _Title=nil;

    _Price=nil;

    _LandArea=nil;
    _Phone=nil;
    _Image1=nil;
    _location=nil;
    _name=nil;
    _email=nil;
    _descrip=nil;
    _type=nil;
    _rooms=nil;
    _washroom=nil;
    _floor=nil;
    _Id=nil;
    _status=nil;



    NSUserDefaults *string = [NSUserDefaults standardUserDefaults];

    NSString *string1 = [string stringForKey:@"country"];

    NSString *string2 = [string stringForKey:@"city"];

    NSString *string3 = [string stringForKey:@"proptype"];

    NSString *string4 = [string stringForKey:@"propstatus"];

    NSString *string5 = [string stringForKey:@"proparea"];

    NSString *string6 = [string stringForKey:@"area"];

    NSString *string7 = [string stringForKey:@"min"];

    NSString *string8 = [string stringForKey:@"max"];




    if ([string4 isEqualToString:@"Property Status"]) {

        string4 = @"";
    }


    if ([string3 isEqualToString:@"Property Type"]) {

        string3 = @"";
    }


    if([string5 isEqualToString:@"Property Area"]){

        string5 = @"";


    }
    else{

        //concatenating strings
    NSString *strappend=[NSString stringWithFormat:@"%@ %@",string6,string5];

    string5 = strappend;
    }

    if ([string7 isEqualToString:@""] && [string8 isEqualToString:@""]) {

        string7 = @"";
        string8 = @"";
    }


    NSLog(@"AREA : %@", string5);


    NSLog(@"country %@",string1);

    NSLog(@"city %@",string2);

    NSLog(@"property_type %@",string3);

    NSLog(@"propertystatus %@",string4);

    NSLog(@"propertyarea %@",string5);

    NSLog(@"area %@",string6);

    NSLog(@"min %@",string7);

    NSLog(@"max %@",string8);


    CustomTableViewCell *check=[[CustomTableViewCell alloc]init];

    check.indicator.hidden=NO;

    [check.indicator startAnimating];

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration 

defaultSessionConfiguration];

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:

 defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

    NSString *urlLinkA=@"My URL";


    NSURL * url = [NSURL URLWithString:urlLinkA];

    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];


    NSString *parameters = [NSString 

stringWithFormat:@"country=%@&city=%@&propertytype=%@&propertstatus=%@&propertyare

a=%@",string1,string2,string3,string4,string5];

    NSLog(@"parameter %@",parameters);

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDataTask * dataTask =[defaultSession 

dataTaskWithRequest:urlRequest
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                           [check.indicator stopAnimating];

                                                           NSLog(@"Response:%@ %@\n", response, error);
                                                           if(error == nil)
                                                           {



                                                               NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

                                                               NSLog(@"MY Response %@ ", dictionary);

                                                               NSArray *results = [dictionary objectForKey:@"property_data"];
                                                               NSLog(@"RESULT %@ ", results);




                                                               _City = [NSMutableArray array];
                                                               _Phone = [NSMutableArray array];
                                                               _LandArea = [NSMutableArray array];
                                                               _location = [NSMutableArray array];
                                                               _name = [NSMutableArray array];
                                                               _email = [NSMutableArray array];
                                                               _descrip = [NSMutableArray array];
                                                               _type = [NSMutableArray array];
                                                               _rooms = [NSMutableArray array];
                                                               _washroom = [NSMutableArray array];
                                                               _floor = [NSMutableArray array];
                                                               _Id = [NSMutableArray array];
                                                               _Price = [NSMutableArray array];
                                                               _Title = [NSMutableArray array];

                                                               for (NSDictionary * oneCustomer in results)
                                                               {
                                                                   // Create a new Customer record

                                                                   StringData * newCustomer = [[StringData alloc] init];



                                                                   newCustomer.title1 = [oneCustomer objectForKey:@"property_title"];
                                                                   NSLog(@"ID: %@ ", newCustomer.title1);
                                                                   newCustomer.Area = [oneCustomer objectForKey:@"land_area"];
                                                                   NSLog(@"Country: %@ ", newCustomer.Area);
                                                                   newCustomer.images = [oneCustomer objectForKey:@"images"];

                                                                   NSLog(@"IMAGE: %@ ", newCustomer.images);

                                                                   newCustomer.City = [oneCustomer objectForKey:@"city"];
                                                                   NSLog(@"CITY: %@ ", newCustomer.City);

                                                                   newCustomer.phoneno = [oneCustomer objectForKey:@"dealer_phone"];
                                                                   NSLog(@"ID: %@ ", newCustomer.phoneno);

                                                                   newCustomer.location = [oneCustomer objectForKey:@"location"];
                                                                   NSLog(@"ID: %@ ", newCustomer.location);

                                                                   newCustomer.property_description = [oneCustomer objectForKey:@"property_description"];
                                                                   NSLog(@"ID: %@ ", newCustomer.property_description); 

                                                                   newCustomer.property_type = [oneCustomer objectForKey:@"property_type"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.property_type); 

                                                                   newCustomer.property_id = [oneCustomer objectForKey:@"property_id"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.property_id); 

                                                                   newCustomer.status_property = [oneCustomer objectForKey:@"status_property"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.status_property); 

                                                                   newCustomer.status = [oneCustomer objectForKey:@"status"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.status); 

                                                                   newCustomer.dealer_email = [oneCustomer objectForKey:@"dealer_email"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.phoneno); 

                                                                   newCustomer.dealer_name = [oneCustomer objectForKey:@"dealer_name"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.dealer_name); 

                                                                   newCustomer.bath = [oneCustomer objectForKey:@"bathrooms"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.bath); 

                                                                   newCustomer.rooms = [oneCustomer objectForKey:@"rooms"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.rooms); 

                                                                   newCustomer.floors = [oneCustomer objectForKey:@"floors"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.floors); 

                                                                   newCustomer.country = [oneCustomer objectForKey:@"country"]; 
                                                                   NSLog(@"ID: %@ ", newCustomer.country); 

                                                                   newCustomer.Price = [oneCustomer objectForKey:@"price"];
                                                                   NSLog(@"ID: %@ ", newCustomer.Price);

                                                                   // Add our new Customer record to our NSMutableArray 
                                                                   [_Title addObject:newCustomer]; 


                                                                   NSString *imageURL = @"http://www.pk.house/frontend/propertyimages/"; 


                                                                   NSString *string3 = [imageURL stringByAppendingString:newCustomer.images]; 

                                                                   newCustomer.images = string3; 

                                                                   NSLog(@"Image URL %@",string3); 


                                                                   // addObject:[oneCustomer objectForKey:@"property_title"]];
                                                                  // NSLog(@"TET %@",_Title);


                                                                   [_Price addObject:[oneCustomer objectForKey:@"price"]]; 
                                                                   NSLog(@"TET %@",_Price);

                                                                   [_Image1 addObject:[oneCustomer objectForKey:@"images"]];
                                                                   NSLog(@"TET %@",_Image1);

                                                                   [_City addObject:[oneCustomer objectForKey:@"city"]];
                                                                   NSLog(@"TET %@",_City);

                                                                   [_Phone addObject:[oneCustomer objectForKey:@"dealer_phone"]];
                                                                   NSLog(@"TET %@",_Phone);

                                                                   [_name addObject:[oneCustomer objectForKey:@"dealer_name"]];
                                                                   NSLog(@"TET %@",_name);

                                                                   [_email addObject:[oneCustomer objectForKey:@"dealer_email"]];
                                                                   NSLog(@"TET %@",_email);

                                                                   [_rooms addObject:[oneCustomer objectForKey:@"rooms"]];
                                                                   NSLog(@"TET %@",_rooms);

                                                                   [_washroom addObject:[oneCustomer objectForKey:@"bathrooms"]];
                                                                   NSLog(@"TET %@",_washroom);

                                                                   [_floor addObject:[oneCustomer objectForKey:@"floors"]];
                                                                   NSLog(@"TET %@",_floor);

                                                                   [_type addObject:[oneCustomer objectForKey:@"price"]];
                                                                   NSLog(@"TET %@",_type);

                                                                   [_descrip addObject:[oneCustomer objectForKey:@"property_description"]];
                                                                   NSLog(@"TET %@",_descrip);

                                                                   [_LandArea addObject:[oneCustomer objectForKey:@"land_area"]];
                                                                   NSLog(@"TET %@",_LandArea);

                                                                   [_Id addObject:[oneCustomer objectForKey:@"property_id"]];
                                                                   NSLog(@"TET %@",_Id);


                                                                   [_location addObject:[oneCustomer objectForKey:@"location"]];
                                                                   NSLog(@"TET %@",_location);

                                                                                                                              }
                                                               dispatch_async(dispatch_get_main_queue(), ^{
                                                                   // This code will run once the JSON-loading section above has completed.


                                                                   [self.tableView reloadData];
                                                               });




                                                               NSString *status=[dictionary valueForKey:@"property_data"];
                                                               NSLog(@"Status:%@",status);

                                                                                                                      }
                                                           else{
                                                               NSLog(@"network error:");


                                                           }

                                                       }];
    [dataTask resume];


}

2 个答案:

答案 0 :(得分:0)

试试这个 -

  dispatch_async(dispatch_get_main_queue(), ^{
         // This block will be executed asynchronously on the main thread.
            [check.indicator stopAnimating];
       });

答案 1 :(得分:0)

您可以使用以下代码:

[self startYourAnimation];
[self performSelector:@selector(loadCustomerRecords) withObject:nil afterDelay:0.3];

当您尝试加载或处理其他内容时,它将帮助您显示动画。