我必须创建一个包含 2 个自定义单元格 UITableView
和 RestTime
的 ExerciseTime
。在 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... 。谁能帮我找出问题所在以及解决方法。
最佳答案
我发现了问题,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/