Search code examples
iosobjective-cuitableviewcustom-cellnsindexpath

UITableViewCell is not in the right order after creating


I have to create an UITableView that contains 2 custom cells RestTime and ExerciseTime. That after an ExerciseTime is a RestTime cell. Here is the design of my tableView:

Design TableView

And here is my implement code:

-Cell's height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // RestTime
        if (indexPath.row % 2 == 1) {
            return 40.0f;
        }

        // ExerciseTime
        else {
            return 65.0f;
        }
    }

-Number of Cells

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return (self.preset.blocks.count * 2) - 1;
    }

-Cell for row

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                {

                    if(indexPath.row % 2 == 1) {
                    RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier  forIndexPath:indexPath];
                    RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
                    //CustomCell
                    return restTimeCell;
                }else{
                    ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier  forIndexPath:indexPath];

                    //Cell index
                    int index = (int)(indexPath.row / 2);
                    //exerciseTimes is a NSSet
                    ExerciseTime *exerciseTime = [self.preset.exerciseTimes.allObjects objectAtIndex:index];

                    //CustomCell
                    return exerciseTimecell;
                }
                return nil;
 }

And the output is this :

Output TableView

I have tried to covert my NSSet to NSMutableArray and its still now working.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                    {

                        if(indexPath.row % 2 == 1) {
                        RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier  forIndexPath:indexPath];
                        RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
                        //CustomCell
                        return restTimeCell;
                    }else{
                        ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier  forIndexPath:indexPath];


                        NSMutableArray *array = [[self.preset.exerciseTimes allObjects] mutableCopy];
                        int index = (int)(indexPath.row / 2);
                        ExerciseTime *exerciseTime = [array objectAtIndex:index];

                        //CustomCell
                        return exerciseTimecell;
                    }
                    return nil;
     }

As you can see that all the ExerciseCell is not in the right order. I cant figure out why it isn't in the right index after the cell is created. I want the order is sorted by the time its created not alphabet abcdef... or 123456... . Can anyone help me to find out what is the problem and how to tackle it.


Solution

  • I had found the problem and it was the NSSet is not sorted at the beginning and I sorted it with @"createdTime" and my problem was solved.

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"createdTime" ascending:YES];
    NSArray *sortedArray = [self.exerciseTimes sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    
    Exercise *exerciseTime = (Exercise *)[sortedArray objectAtIndex:indexPath.row/2];