UITableView not updating correctly based on UISegmentedControl

3545 views ios
1

I have a table which is initially populated from viewDidLoad:

- (void)viewDidLoad {
  [super viewDidLoad];

  eventnameArray = [[NSMutableArray alloc]init];
  organizatorArray = [[NSMutableArray alloc]init];
  sportArray = [[NSMutableArray alloc]init];
  eventImageArray = [[NSMutableArray alloc]init];
  dateandtimeArray = [[NSMutableArray alloc]init];
  vacantsArray = [[NSMutableArray alloc]init];

  // We get all the events created by CurrentUser from Parse

  PFQuery *query = [PFQuery queryWithClassName:@"Event"];
  //[query orderByAscending:@"DateAndTime"];
  [query whereKey:@"Organizator" equalTo:PFUser.currentUser.username];
  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // Do something with the found objects
        for (NSObject *object in objects){

            [self.eventnameArray addObject:[object valueForKey:@"EventName"]];
            [self.organizatorArray addObject:[object valueForKey:@"Organizator"]];
            [self.sportArray addObject:[object valueForKey:@"SportName"]];


            //create a date formatter
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

            //set its format as the required output format
            [formatter setDateFormat:@"dd-MM-yyyy 'at' HH:mm a"];

            //get the output date as string
            NSString *selectedDateString = [formatter stringFromDate:[object valueForKey:@"DateAndTime"]];
            [self.dateandtimeArray addObject:selectedDateString];

            [self.vacantsArray addObject: [(NSNumber*)[object valueForKey:@"Vacants"] stringValue]];

        }    
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

    [self.tableView reloadData];
}];
}

The tableview has a UISegmented control which I use to show different data. The segmented control toggle is defined as :

- (IBAction)segmentedControlChanged:(id)sender{
   switch (eventssegmentedControl.selectedSegmentIndex) {
    case 0:

        [self viewDidLoad];
        break;
    case 1:

        [self loadjoinedevents];
        break;

    default:
        break;
  }
}

And I also post the ldadjoinedevents method because the problem could be spotted there:

-(void)loadjoinedevents{

PFQuery *query = [PFQuery queryWithClassName:@"Event"];
// [query orderByAscending:@"DateAndTime"];
[query whereKey:@"Participants" containsString:PFUser.currentUser.username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // Do something with the found objects
        for (NSObject *object in objects){

            [self.eventnameArray addObject:[object valueForKey:@"EventName"]];
            [self.organizatorArray addObject:[object valueForKey:@"Organizator"]];
            [self.sportArray addObject:[object valueForKey:@"SportName"]];

            //create a date formatter
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

            //set its format as the required output format
            [formatter setDateFormat:@"dd-MM-yyyy 'at' HH:mm a"];

            //get the output date as string
            NSString *selectedDateString = [formatter stringFromDate:[object valueForKey:@"DateAndTime"]];

            [self.dateandtimeArray addObject:selectedDateString];

            [self.vacantsArray addObject: [(NSNumber*)[object valueForKey:@"Vacants"] stringValue]];

        }    
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

    [self.tableView reloadData];
  }];  
}

My problem:

The UITableViewController initially loads the correct data. If I tap on the "case 1" from the segmented control, it will preserve the rows loaded initially + the ones that "loadjoinedevents" loads, resulting in more rows than desired.

What I tried:

I thought about setting the DataSource to nil, but numberOfRowsInSection will return 0.

answered question

1 Answer

10

As you always append here

[self.vacantsArray addObject: [(NSNumber*)[object valueForKey:@"Vacants"] stringValue]];

you need to remove the objects before the add so before the for loop do

posted this

Have an answer?

JD

Please login first before posting an answer.