ios - 创建后 UITableViewCell 的顺序不正确

标签 ios objective-c uitableview custom-cell nsindexpath

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

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

输出是这样的:

Output TableView

我已经尝试将我的 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... 。谁能帮我找出问题所在以及解决方法。

最佳答案

我发现了问题,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];

关于ios - 创建后 UITableViewCell 的顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40376666/

相关文章:

iOS在中国的网络访问权限

ios - 如何从 ios 中的 Xib 文件加载自定义 UITableViewCells

objective-c - 检查应用程序是否安装在/Application目录内

ios - 'NSInternalInconsistencyException',原因 : 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update'

ios - 在 UITableViewCell 中滑动 UIView

ios - 如何将 UISwitch 放置在 UITableViewCell accessoryView 中?

ios - 重构 iOS ViewController 使其无需 Nib 即可工作

ios - Sprite Kit with Swift "use of unresolved identifier ' size'”示例代码

ios - 如果应用在 TestFlight beta 审核中被拒绝,是否会影响同一版本的应用商店审核?

IOS,如何将语言环境强制为一种语言