【发布时间】:2013-05-10 01:31:48
【问题描述】:
这里我想要一个多级表格视图。 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];
}
}
}
这里有一个示例图片:
【问题讨论】:
-
你能提供一些图片吗
-
检查这个答案。它有效.. stackoverflow.com/a/34586224/3908884
标签: iphone ios objective-c uitableview