创建后 UITableViewCell 的顺序不正确

UITableViewCell is not in the right order after creating

我必须创建一个 UITableView,其中包含 2 个自定义单元格 RestTimeExerciseTime.在 ExerciseTime 之后是一个 RestTime 单元格。 这是我的 tableView 的设计:

这是我的实现代码:

-单元格高度

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

        // ExerciseTime
        else {
            return 65.0f;
        }
    }

-单元格数

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

-行单元格

    - (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;
 }

输出是这样的:

我试图将我的 NSSet 转换为 NSMutableArray,它现在仍在工作。

    - (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;
     }

如您所见,所有 ExerciseCell 的顺序都不正确。我无法弄清楚为什么在创建单元格后它不在正确的索引中。我希望订单按其创建时间排序,而不是字母表 abcdef... 或 123456... 。谁能帮我找出问题所在以及解决方法。

请找到我的工作代码和截图

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *messageTableView;

@property (retain, nonatomic) NSMutableArray *datasourceArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

    [self.messageTableView registerNib:[UINib nibWithNibName:@"RestTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"RestTime"];
    [self.messageTableView registerNib:[UINib nibWithNibName:@"ExerciseTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"ExerciseTime"];

    self.messageTableView.separatorColor = [UIColor blackColor];
    self.messageTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    self.datasourceArray = [[NSMutableArray alloc]init];
    for (int i = 1; i <= 20; i++) {
        [self.datasourceArray addObject:[NSString stringWithFormat:@"%d%d%d%d",i,i,i,i]];
    }


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

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

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

    if (indexPath.row % 2 == 1) {
        RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RestTime"  forIndexPath:indexPath];
            //Access you object from array like this

        return restTimeCell;
    }
    else {
        ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ExerciseTime"  forIndexPath:indexPath];
            //Access you object from array like this
        int index = (int)(indexPath.row / 2);

        exerciseTimecell.exerciseName.text = [self.datasourceArray objectAtIndex:index];
        return exerciseTimecell;
    }
}
@end

截图

NSSet只是一个集合,对象之间没有顺序。如果您想保留顺序,可以使用 NSArray(或 NSMutableArray)代替 NSSet

我找到了问题,NSSet 开始时没有排序,我用@"createdTime" 排序,我的问题就解决了。

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"createdTime" ascending:YES];
NSArray *sortedArray = [self.exerciseTimes sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

Exercise *exerciseTime = (Exercise *)[sortedArray objectAtIndex:indexPath.row/2];