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

标签 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);

        }

    }
}

This works really well and upon selection of a UITableViews section the rows are expanded and populated with the correct data and when the same section is selected the rows are removed and appear collapsed.

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

我曾尝试将选定的部分索引路径存储到一个数组中,并根据此值删除行,但我认为我的做法是错误的,因为我遇到了断言失败。

所以我的问题如下:-

如何在选择另一个部分时从 uitableviews 部分自动折叠(删除行)

提前感谢您的帮助

托马斯

最佳答案

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

- (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];
            }


        }

    }

}
}

`

关于ios - 从上一个选定部分的 UITableView 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486934/

相关文章:

ios - 核心定位延迟

ios - 在 iOS 7/Objective-C 中,如何以编程方式将 UIPickerView 捕捉到它所在的父 View 的底部?

objective-c - 如何将此返回字典的方法从 Swift 转换为 Objective-C?

ios - 自动布局或自动调整大小

swift - 如何在 Swift 2 的 UITextView 中将大型静态文本的某些部分设为粗体或斜体

ios - UIButton - NSInternalInconsistencyException - 无法使具有标识符 Cell2 的单元格出队

ios - 如何重新创建支持静态单元格的 UITableViewController?

iOS Swift 不支持的 URL

iphone - NSMetadataQuery 在完成(或未完成)时不发送通知

ios - 从我的应用跟踪其他应用 - iOS