tableview重新加载不调用tableview委托方法

时间:2015-12-06 07:14:18

标签: objective-c uitableview

enter image description here我在TrendingEventsTableViewController类中有一个tableview和一个实现选择器视图的selectRadiusViewController。在从pickerview中选择一个值时,TrendingEventsTableViewController上的数据应该使用ParseModelClass中的新数据重新加载。当NSlog检查时,在tableview reloaddata之后没有调用tableview委托方法。

//pickerview method of selectRadiusViewController

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *result = [NSString stringWithFormat:@"Radius Chosen is %@", Radii[row]];


    self.RadiusChosen.text=result;

    ParseDataModel *obj = [[ParseDataModel alloc]init];
    TrendingListViewController *tobj = [[TrendingListViewController alloc]init];
   tobj.RadiusInput = true ;// ** true indicates table view should be modified
    [obj CalRadius:6000]; // call this method to populate modified array
    NSLog(@"Contrl returned after calRad func call with count: %lu", obj.modEventNames.count);


   // dispatch_async(dispatch_get_main_queue(), ^{
        [tobj.TrendingTableView reloadData];
   //});

}

//callRadius method in ParseDataModelClass to get the new data to refresh the tableview

-(void) CalRadius:(int) rad

{  
    CLLocation *Userlocation = [[CLLocation alloc] initWithLatitude:57.052443 longitude:9.910623];

    PFGeoPoint *ULocation =  [[PFGeoPoint alloc]init];
    ULocation.latitude=57.052443;
    ULocation.longitude=9.910623;

    // PFGeoPoint *ULocation =[PFGeoPoint geoPointWithLatitude:40.75060000 longitude:73.99360000];

    self.GeoPointsRadii = [[NSMutableArray alloc] init];
    self.modEventNames =  [[NSMutableArray alloc] init];

  //  --------<filtering radius array >---------

    PFQuery *query = [PFQuery queryWithClassName:@"EventInfo"];
    [query whereKey:@"EventLocation" nearGeoPoint:ULocation withinKilometers:6000];

     self.GeoPointsRadii = [query findObjects];

    for(int i=0; i< [ self.GeoPointsRadii  count] ; i++)
    {

            PFObject *tempObj = self.GeoPointsRadii [i];
            self.modEventNames[i]=tempObj[@"EventName"];

    }

  }

//TrendingEventsTableViewController

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"No of sections called");
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection called");

    // Return the number of rows in the section.
    if(self.RadiusInput==false)
    return self.myGuys.count;
    else{
        ParseDataModel *obj=[[ParseDataModel alloc]init];
       return  obj.modEventNames.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"TABLE VIEW method called");

      static NSString *CellIdentifier = @"Cell"; // reuse identifier

    // check if we can reuse a cell from row that just went off screen
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // create new cell, if needed
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if(self.RadiusInput==false)
    {
        NSLog(@"TABLE VIEW FILLING NORMALLY WITH myGuys");
    UIImage *image= [UIImage imageNamed:@"garfield(1).jpg"];
    cell.imageView.image= image;
    // set text attibute of cell
    cell.textLabel.text = [self.myGuys objectAtIndex:indexPath.row];
    }

    else{
        NSLog(@"TABLE VIEW RELOADING");

        UIImage *image= [UIImage imageNamed:@"garfield(1).jpg"];
        cell.imageView.image= image;
        // set text attibute of cell
        ParseDataModel *obj=[[ParseDataModel alloc]init];

        cell.textLabel.text = [obj.modEventNames objectAtIndex:indexPath.row];
    }
    // set accessory type to standard detail disclosure indicator
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

1 个答案:

答案 0 :(得分:1)

要从另一个类重新加载表,您可以使用 NSNotification 让表的视图控制器知道何时重新加载数据。

在表的视图控制器中,在viewDidLoad下,添加:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadData" object:nil];

在表视图控制器中添加此方法:

- (void)reloadTable:(NSNotification *)notification { 
    [self.TrendingTableView reloadData]; 
}

最后,在选择器视图控制器中触发通知(而不是[tobj.TrendingTableView reloadData];):

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];
相关问题