【问题标题】:tableview reload not calling the tableview delegate methodstableview 重新加载不调用 tableview 委托方法
【发布时间】:2015-12-06 18:26:58
【问题描述】:

我在 TrendingEventsTableViewController 类中有一个 tableview 和一个实现选择器视图的 selectRadiusViewController。从选择器视图中选择值后,TrendingEventsTableViewController 上的数据应使用 ParseModelClass 中的新数据重新加载。当 NSlogged 进行检查时,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;
}

【问题讨论】:

  • 只是确定,你设置了 吗?另外,您是否确定实际调用了 reloadData 方法?
  • @RoyKronenfeld 是的,我做到了
  • @interface TrendingListViewController : UITableViewController
  • 你在创建表的时候tobj.TrendingTableView.delegate = self吗?
  • 我只是将故事板上的数据源和委托设置为 TrendingViewController

标签: objective-c uitableview


【解决方案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];

【讨论】:

  • @askatral 我的荣幸!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多