-1

I have a very strange behavior of my UITableView. When I call it first time after starting the app all data are shown as they should. When I move to another Viewcontroller and come back to the tableview it only shows half the data. I figured out that [tableview reloaddata] in viewwillappear causes the issue. If I comment it out, all data stay as they were. Therefore I suppose that something is going wrong while retrieving the data when coming back. I need to tell that the data are coming from 2 different arrays which fill labels in a custom UITableViewCell. As I have some other difficulties with the setup of this cell (labels don't work compliant to the set constraints) first I thought maybe the labels are out of view. But now I'm pretty sure that the data of one of the arrays are gone.

Does anyone know, if there is a difference of loading data for the first time and reload data? Is it maybe a timing problem and the tableviewcontroller does not have enough time to retrieve the data? If yes what can I do against it? Could there are other reasons why it doesn't work? Please tell me if I should post some code. I did not know what to show to solve the issue.

Thank you in advance for your appreciate help.

`

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    [self fetchAllTermine]; //method to retrieve data from array #1
    NSLog(@"viewWillAppear: %@", [self fetchAllTermine]);//logfile always shows data
    events = (NSMutableArray*) [self getEventsFromCalendar]; //method to retrieve data from array #2  , events is a global variable  
[self.tableView reloadData];//after reloadData data from array #1 are missing

}

-(NSMutableArray*)fetchAllTermine
{
    NSFetchRequest<Termine *> *tfetchRequest = [Termine fetchRequest];
    NSError *error ;
    NSMutableArray * fetchedTermine = [[[self managedObjectContext] executeFetchRequest:tfetchRequest error:&error]mutableCopy];
    self.termineArray = [[[self managedObjectContext] executeFetchRequest:tfetchRequest error:&error]mutableCopy];
  //  NSLog(@"fetchAllTermine in MonatTVC: %@", self.termineArray);
    return fetchedTermine;
}



-(NSArray*)getEventsFromCalendar
{

        [[EKEventStore new] requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            if (granted) {
                NSLog(@"Access to calendar granted");
            } else {
                NSLog(@"No access to calendar");
            }
        }];

        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSArray *calendars = [[EKEventStore new] calendarsForEntityType:EKEntityTypeEvent];

        NSDateComponents *tomorrowComponents = [[NSDateComponents alloc] init];
        tomorrowComponents.month = 0;
        NSDate *tomorrow = [calendar dateByAddingComponents:tomorrowComponents
                                                     toDate:[NSDate date]
                                                    options:0];

        // Create the end date components
        NSDateComponents *afterTomorrowComponents = [[NSDateComponents alloc] init];
        afterTomorrowComponents.month = +1;
        NSDate *afterTomorrow = [calendar dateByAddingComponents:afterTomorrowComponents
                                                          toDate:[NSDate date]
                                                         options:0];


        NSPredicate *predicate = [[EKEventStore new] predicateForEventsWithStartDate:tomorrow
                                                                             endDate:afterTomorrow
                                                                           calendars:calendars];

        NSArray *eventListe = [[EKEventStore new] eventsMatchingPredicate:predicate];
    return eventListe;

}

`

coming back to the view

first call of the view with all data shown

3 Answers3

0

You shoud copy array data to show it on the cell. Otherwise you will not get the total data you wanted to show.

iMark
  • 34
  • 5
0

After a quick glance at what can be wrong with your code. Having reloadData in viewWillAppear() is not ok:

  1. First of all, viewWillAppear is called when the view is not yet visible and reloadData causes redraw and other operations that depend/need the visible view (for example, heights of cells);
  2. Second, the table will be redrawn anyways when the view becomes visible, so your reloadData is useless here;
  3. viewWillAppear() is called every time your view becomes visible after closing the child view controller. Do you really want the table to reload with flickering and scrolling at the origin every time in a situation like that?

Except this, there is not enough code to see what else can be wrong. How the table is initialized in viewDidLoad, how cells are created and populated, etc.

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33
  • Cynichniy Bandera which method would you recommend to place reloadData in? The call of the method should bring newly entered data in the tableview. If I don't call it, new data only appear after restarting the app. – Debhelapara Aug 20 '18 at 11:12
  • Sigh... Its not that I can recommend, its more about doing it right. Man, you definitely need to read some kind of "IOS UI programming for dummies". You need it, trust me ) – Cynichniy Bandera Aug 20 '18 at 13:12
  • This this one: https://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ – Cynichniy Bandera Aug 20 '18 at 13:16
0

Thanks to all who answered my question. Now I found the solution to my problem. After first initializing I overwrote the terminData-Array everytime when I was coming back to the tableview. Everything works fine now and I can use both arrays parallel to fill in data in the tableview.

BR

Debhelapara