从上一个选定部分删除UITableView中的行

时间:2015-05-27 15:32:24

标签: ios objective-c uitableview

目前我有一个UITableView,可以使用当前代码进行扩展和折叠。

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section{
    if (section>=0) return YES;
    return NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger numberofRows = 0;

    numberofRows = nameArray.count;

    if (numberofRows != 0){
        self.mainTableView.hidden = false;
    }

    return numberofRows;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            // only first row toggles exapand/collapse
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

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

            NSMutableArray *tmpArray = [NSMutableArray array];

            if (currentlyExpanded)
            {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];

            }
            else
            {
                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
            }

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

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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

                cell.accessoryView = [ALCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:ALCustomColoredAccessoryTypeDown];

            }
            else
            {
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
                cell.accessoryView =  [ALCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:ALCustomColoredAccessoryTypeUp];

            }
        }
        else {
            NSLog(@"Selected Section is %ld and subrow is %ld ",(long)indexPath.section ,(long)indexPath.row);

        }

    }
}

这非常有效,在选择UITableViews部分后,行将展开并填充正确的数据,当选择相同的部分时,行将被删除并显示为折叠状态。

但是我想要做的是以某种方式自动折叠上一个选定的部分,并在用户选择新的indexpath.section时删除上一部分中的行。

我已经尝试将选定的部分索引路径存储到一个基于此值删除行的数组中,但我认为当我得到断言失败时,我会以错误的方式处理它。

所以我的问题如下: -

如何在选择其他部分时从uitableviews部分自动折叠(删除行)

感谢您的提前帮助

托马斯

1 个答案:

答案 0 :(得分:1)

当我为我的应用程序编写代码时,我遇到了这个问题,我创建了这篇没有任何评论或回答的帖子...... 当我点击其他部分并展开该部分的行时,您可以使用我为我编写的代码来折叠以前扩展的部分行,此代码可供我试用...     `

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
if (section>=0) return YES;

return NO;
}




static NSInteger Value=0;

static NSInteger Value2=0;
static CFIndex indexPre=0;
static NSMutableArray *preArray;
static NSIndexPath *path;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
    if (!indexPath.row)
    {
        TableViewCell *cell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell.hideButton.hidden=NO;
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSUInteger index=[expandedSections firstIndex];

if ([expandedSections indexGreaterThanIndex:index])
        {
            NSLog(@"%lu hhhwww", index);
        }

        NSInteger section = indexPath.section;

      static  NSInteger rows;
        static  NSInteger PreRows;
        NSLog(@"%lu MY INDEXES1",(unsigned long)[expandedSections count]);
        NSMutableArray *tmpArray = [NSMutableArray array];
        preArray = [NSMutableArray array];

        BOOL currentlyExpanded = [expandedSections containsIndex:section];


         if (currentlyExpanded)
        {
            rows = [self tableView:tableView numberOfRowsInSection:section];
            [expandedSections removeIndex:section];
            Value=0;
            Value2=0;
            cell.hideButton.hidden=YES;
        }
        else
        {
            if (Value==0)
            {

                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
                PreRows = [self tableView:tableView numberOfRowsInSection:section];
                indexPre=indexPath.section;
                Value=1;

            }
            else if (Value==1)
            {
                Value2=1;
                NSLog(@"indexPre == %ld %@ my indexxxxx", indexPre, expandedSections);
                [expandedSections removeIndex:indexPre];
                   NSLog(@"indexPre == %ld %@ my indexxxxx", indexPre, expandedSections);
                [self.tableView reloadData];
                [expandedSections addIndex:section];
                   NSLog(@"indexPre == %ld %@ my indexxxxx", indexPre, expandedSections);
                rows=[self tableView:tableView numberOfRowsInSection:section];
            }


        }

        NSLog(@"%@ MY INDEXES",expandedSections);
        if (Value2==1)
        {
            for (int i=1; i<PreRows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                               inSection:indexPre];
                [preArray addObject:tmpIndexPath];
            }
            NSLog(@"my arrray %@", preArray);
            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                               inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }
            NSLog(@"my arrray  tmparray%@", preArray);

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



        if (currentlyExpanded )
        {


            [tableView deleteRowsAtIndexPaths:tmpArray
                             withRowAnimation:NO];

            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];


        }
        else if(Value==1)
        {
            if (Value2==1)
            {

                [tableView beginUpdates];

                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:NO];


                cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

                [tableView endUpdates];
                [tableView beginUpdates];

                [tableView endUpdates];

                indexPre=indexPath.section;
            }
            else
            {

            [tableView insertRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];
            cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
            }


        }

    }

}
}

`