滚动时,单元配件视图消失

时间:2014-05-21 11:27:58

标签: ios objective-c uitableview

我有一个可扩展的列表,当我滚动一些附件视图消失时,我不明白为什么。 可扩展行具有附件视图(+或 - )。其余的行没有附件视图。

我尝试了[table reloadTable]但没有尝试,我尝试了一些关于可恢复细胞的事情,但也没有尝试过。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    if ([self tableView:tableView canCollapseSection:indexPath.section]) // Todas las filas se pueden expandir
    {

        NSMutableArray *aux = [[NSMutableArray alloc] init];
        InfoTitulacion_DTO *rowData = [self.asignaturas objectAtIndex:indexPath.section];

        [aux addObject: rowData];
        [aux addObjectsFromArray: rowData.asignaturas];

        if (!indexPath.row)
        {
            if ([rowData.nombre isEqualToString:@"optativos"]) {
                cell.textLabel.text = AMLocalizedString(@"optativos", @"");
            }else{

                cell.textLabel.text = [AMLocalizedString(rowData.nombre, @"") stringByAppendingString:AMLocalizedString(@"cursoLabel", @"")];
            }
            cell.detailTextLabel.text=@"";

            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
            }else{
                cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
            }
            cell.indentationLevel = 0;


            if ([expandedSections containsIndex:indexPath.section])
            {

                UIImage *btnImage = [UIImage imageNamed:@"Minus-256.png"];
                [abrirButton setImage:btnImage forState:UIControlStateNormal];
                cell.accessoryView = abrirButton;

            }
            else
            {
                abrirButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
                cell.accessoryView = abrirButton;

            }
        }
        else
        {
            // all other rows
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                cell.textLabel.font = [UIFont systemFontOfSize:15];
            }else{
                cell.textLabel.font = [UIFont systemFontOfSize:13];
            }

            AsignaturaTitulacion_DTO *asig_dto = [[AsignaturaTitulacion_DTO alloc] initWithAsignaturaTitulacion:[aux objectAtIndex:indexPath.row]];

            cell.textLabel.text = asig_dto.nombre;
            cell.detailTextLabel.text = [[[[AMLocalizedString(@"creditosAsig", @"") stringByAppendingString:asig_dto.creditos] stringByAppendingString:@"   "] stringByAppendingString:AMLocalizedString(@"caracter", @"")] stringByAppendingString:asig_dto.caracter];
            cell.accessoryView = nil;
            cell.indentationLevel = 1;
            cell.textLabel.numberOfLines = 2;
        }
    }

    return cell;
}

另一个功能:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self tableView:table canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            [self.table beginUpdates];

            UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath];

            // only first row toggles exapand/collapse
            [table deselectRowAtIndexPath:indexPath animated:YES];

            NSInteger section = indexPath.section;
            BOOL currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;

            NSMutableArray *tmpArray = [NSMutableArray array];

            if (currentlyExpanded)
            {
                rows = [self tableView:table numberOfRowsInSection:section];
                [expandedSections removeIndex:section];
                UIImage *btnImage = [UIImage imageNamed:@"Minus-256.png"];
                [abrirButton setImage:btnImage forState:UIControlStateNormal];
                cell.accessoryView = abrirButton;
            }
            else
            {
                [expandedSections addIndex:section];
                rows = [self tableView:table numberOfRowsInSection:section];
                abrirButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
                cell.accessoryView = abrirButton;
            }


            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }



            if (currentlyExpanded)
            {
                [table deleteRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];

                abrirButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
                cell.accessoryView = abrirButton;

            }
            else
            {
                [table insertRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];
//                abrirButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
                UIImage *btnImage = [UIImage imageNamed:@"Minus-256.png"];
                [abrirButton setImage:btnImage forState:UIControlStateNormal];
                cell.accessoryView = abrirButton;

            }

            [self.table endUpdates];

        }
    }
}

图片:

before after

1 个答案:

答案 0 :(得分:2)

您必须在cellForRowAtIndexPath中重置单元格,因为tableview会重复使用单元格。你可能会隐藏一些单元的附件视图,并且它可以被重用。

就像这样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

[self configureCell:cell];
//continue your logic

.... ...

}

- (void) configureCell:(UITableViewCell*) cell{
{
      //Rest all the values of your cell to default. 
}