如何在Objective-c中创建高达n级的多级UITableView



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

级别的数量将在运行时定义。请帮忙,我在Objective-C工作。

试试这个代码。按照我的要求为我工作。

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

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

最新更新