UITableView行顺序

时间:2016-11-07 13:32:52

标签: ios objective-c uitableview

我的tableView有一个奇怪的行为 我在tableView上制作了下拉部分。它看起来像:

enter image description here

但如果我重新打开我的部分行,那么顺序是这样的:

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        NSLog(@"CURRENT ROW = %ld", (long)indexPath.row);
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, cell.frame.size.width - 90, 20)];
        titleLable.text = [NSString stringWithFormat:@"урок %ld", (long)indexPath.row];
        [cell addSubview:titleLable];
        UILabel *percentLable = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width - 40, 20, 50, 20)];
        LessonModel *lesson = [self.lessonsArray objectAtIndex:indexPath.section];
        NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];;
        percentLable.text =[NSString stringWithFormat:@"%@ %%", [childrenArrey[indexPath.row] valueForKey:@"percent"]];
        [cell addSubview:percentLable];
    }
}
return cell; 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
LessonModel *lesson = [self.lessonsArray objectAtIndex:indexPath.section];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];
NSString *uid =  [childrenArrey[indexPath.row] valueForKey:@"id"];
[self makeLessonWithId:uid];
}

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
    BOOL collapsed  = [[self.arrayForBool objectAtIndex:indexPath.section] boolValue];
    collapsed       = !collapsed;
    [self.arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
    [self updateLabelFrame:gestureRecognizer.view.tag];

    NSRange range   = NSMakeRange(indexPath.section, 1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
    [self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
}

1 个答案:

答案 0 :(得分:0)

尝试将值放入else部分:

    if (cell == nil){
          //your code as it is
    }
    else{
          titleLable.text = [NSString stringWithFormat:@"урок %ld", (long)indexPath.row];
          NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];;
          percentLable.text =[NSString stringWithFormat:@"%@ %%", [childrenArrey[indexPath.row] valueForKey:@"percent"]];
    }