【问题标题】:Remove Rows From UITableView from Previous Selected Section从上一个选定部分的 UITableView 中删除行
【发布时间】:2015-09-16 18:26:15
【问题描述】:

目前我有一个 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 部分自动折叠(删除行)

提前感谢您的帮助

托马斯

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案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];
                }
    
    
            }
    
        }
    
    }
    }
    

    `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多