я должен создать UITableView
, который содержит 2 пользовательских ячеек RestTime
и ExerciseTime
. Что после a ExerciseTime
является RestTime
ячейка. Вот дизайн моего Tableview:UITableViewCell не в правильном порядке после создания
И вот мой код реализации:
высота -клеток в
- (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;
}
-Cell для строки
- (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 .... Может ли кто-нибудь помочь мне выяснить, в чем проблема и как ее решить.
@Vinodh Да я следовать этой инструкции, и она имеет эту проблему – VMCuongOnStackOverflow
'allObjects' не гарантирует заказ. Я не знаю, является ли 'blocks'' 'NSSet или иначе, но вы можете проверить логику этого. Из документа: 'Порядок объектов в массиве не определен. ' – Larme
@ Larme это NSSet – VMCuongOnStackOverflow