在这里,我想要一个Multilevel tableview。I was able to create it successfully but when one of the levels is selected, any previously expanded dropdown level should collapse. 我使用了下面的示例代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cellid"];
if (cell==nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.textLabel setText:[[arrtable objectAtIndex:indexPath.row] objectForKey:@"name"]];
[cell setIndentationLevel:[[[arrtable objectAtIndex:indexPath.row] objectForKey:@"level"] intValue]];
return cell;
}
什么时候选择行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"arrtable :%@",arrtable);
NSDictionary *d=[arrtable objectAtIndex:indexPath.row];
NSLog(@"d%@",d);
if([d valueForKey:@"objects"]) {
NSArray *ar=[d valueForKey:@"objects"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[arrtable 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]];
[arrtable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
- (void)miniMizeThisRows:(NSArray*)ar{
for(NSDictionary *dInner in ar ) {
NSUInteger indexToRemove=[arrtable indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:@"objects"];
if(arInner && [arInner count]>0){
[self miniMizeThisRows:arInner];
}
if([arrtable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
[arrtable removeObjectIdenticalTo:dInner];
[testtable deleteRowsAtIndexPaths:[NSArray arrayWithObject:
[NSIndexPath indexPathForRow:indexToRemove inSection:0]
]
withRowAnimation:UITableViewRowAnimationRight];
}
}
}
这里我有一个示例图像: