user3066516
user3066516

Reputation: 21

Not reloading the right data in UITabeView

I wrote this code to reload the UItableView with an events that has the same date as the current date when the user click on todays events UIButton in the main view controller but the problem is the below code is not reloading the right data (it just gives the initial data without comparing the date of the event with the date of the calendar in the IPhone), my data comes from a json file within the project and consists from NSArray of events, each has a different value for each key and one of these keys is the data of that event ("date"), can anyone plz clarify for me why the below code is not returning the right data ??

  @implementation MainViewController {
 NSArray *_events;
 }



.... 

- (IBAction)upcomingEvents:(id)sender {

NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-YYYY"];
NSString *dateString = [dateFormatter stringFromDate:currDate];


for (Events *event in _events){

    if([event.date isEqualToString:dateString]){

       [self.myTableView reloadData];

    }

}


}

Upvotes: 1

Views: 104

Answers (1)

George
George

Reputation: 1465

If you're using a UITableViewDataSource you should make sure that it returns only the events that match your condition [event.date isEqualToString:dateString]

You can do

NSArray * dateEvents = [_events filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Events * event, NSDictionary *bindings)
{
    return [event.date isEqualToString:dateString];
}];

Then you can use dateEvents for your UITableViewDataSource.

Upvotes: 1

Related Questions