Yes, it is the same old reuse problem that caused me to fail again (the last time was over a year ago, it seems). After thinking through the pain, I decided to write it down to prevent future mistakes.

Let’s start with the page that shows the problem:

Well, it’s a monthly calendar with a different list of tasks for each day.

The UICollectionView cell contains a nested UITableView.

The initialization calendar data is fine. After swiping or clicking refresh, the daily task data starts to display incorrectly. Well, as an experienced programmer, two words come to mind at this time of course: reuse.

My initial thinking went something like this: First of all, the outer calendar is correct, so it should not be the UICollectionCell problem, but the UITableViewCell reuse of the UITableView nested in the UICollectionViewCell (which turns out to be wrong).

After I have combed through the logic of the internal nested UITableView, I find no problem.

After wasting some time trying to solve the problem, I began to examine my original thinking.

The daily task list data is passed in the UICollectionCell:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZXCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; NSString *currentTime = [NSString stringWithFormat:@"% @ % @ - % @",array[0],array[1],str]; //self.dataArray is calendar data of a month, iterate to find the date and give the scheduleModel of the date to the cell, and then display the tableView of the cellfor (ScheduleModel *model in self.dataArray) {
        if ([model.VisitTime isEqualToString:currentTime]) {
            cell.scheduleModel = model;
            break; }} // Some other cell operationsreturn cell;
}
Copy the code

Seriously, after looking at the above code for several times, I still don’t think there is any problem, the logic is all ok!! However, experience tells me there must be something wrong here, there must be something wrong with transferring data!!

Later the brain finally not funny, want to understand, so change the following code:

ScheduleModel *currentModel; BOOL hasData = NO;for (ScheduleModel *model in self.dataArray) {
        if ([model.VisitTime isEqualToString:currentTime]) {
            currentModel = model;
            hasData = YES;
            break; }} // Then assign the cellif (hasData) {
        cell.scheduleModel = currentModel;
    }else{
        cell.scheduleModel = nil;
    }
Copy the code

Problem solved.

What is clear may be clear at first. The script in box 1 has unprocessed cells, so there is a problem.

** says simply: CellForItemAtIndexPath :(NSIndexPath *)indexPath: cellForItemAtIndexPath:(NSIndexPath *)indexPath: cellForItemAtIndexPath:(NSIndexPath *)indexPath The problem is that the whole cell is not written in different cases. Like if/else if and no else.

Tomorrow I will go to the Node group to move bricks and do some work. Later I will open a new topic to write Node. Welcome to continue to focus on it.