【问题标题】:How to create a multilevel UITableView upto n level in Objective-c如何在Objective-c中创建最多n级的多级UITableView
【发布时间】:2021-09-21 07:33:48
【问题描述】:

我想创建一个通用的 UITableView 展开和折叠,以便它可以与数组/字典的数组绑定到多级。

级别数将在运行时定义。请帮忙,我正在使用 Objective-C。

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

试试这个代码。根据我的要求为我工作。

------ 展开行 ------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary * d = [self.arForTable objectAtIndex:indexPath.row];
    if([d valueForKey:@"Objects"]) {
        NSArray * ar = [d valueForKey:@"Objects"];
        BOOL isAlreadyInserted = NO;
        
        for(NSDictionary * dInner in ar){
            NSInteger index = [self.arForTable indexOfObjectIdenticalTo:dInner];
            isAlreadyInserted = (index > 0 && index != NSIntegerMax);
            if(isAlreadyInserted) break;
        }
        
        if(isAlreadyInserted) {
            [self miniMizeThisRows:ar];
        }
        else {
            NSUInteger count = indexPath.row+1;
            NSMutableArray * arCells = [NSMutableArray array];
            for(NSDictionary * dInner in ar) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [self.arForTable insertObject:dInner atIndex:count++];
            }
            
            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationFade];
            
            [tableView beginUpdates];
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [tableView endUpdates];
        }
    }
}

------ 最小化行数-----

-(void)miniMizeThisRows:(NSArray *)ar{
    for(NSDictionary * dInner in ar ) {
        NSUInteger indexToRemove = [self.arForTable indexOfObjectIdenticalTo:dInner];
        
        NSArray * arInner = [dInner valueForKey:@"Objects"];
        if(arInner && [arInner count] > 0){
            [self miniMizeThisRows:arInner];
        }
        
        if([self.arForTable indexOfObjectIdenticalTo:dInner] != NSNotFound) {
            [self.arForTable removeObjectIdenticalTo:dInner];
            [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            
            [self.mainTableView beginUpdates];
            [self.mainTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexToRemove-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            [self.mainTableView endUpdates];
        }
    }
}

【讨论】:

  • 根据我的 UITableView 进行一些更改后,这工作正常。非常感谢。
猜你喜欢
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多