【发布时间】:2016-05-11 14:28:21
【问题描述】:
嘿,我是 ios 开发的新手。我想展开和折叠tableview,所以我的代码是:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section != 0)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
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:1]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationTop];
}
}
}
还有我的最小化行方法,比如 as
-(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.tblLeft deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:1]]withRowAnimation:UITableViewRowAnimationBottom];
}
}
}
通过这种方法和编码,我实现了Expand and Collapse tableViewCell,这是可行的。因为它在单击时展开 tableViewCell,并在第二次单击可展开单元格时将其折叠。
但我想这样工作:单击任何可展开单元格时,其他可展开单元格将自动折叠。
谁能帮我解决这个问题?
提前致谢。
【问题讨论】:
标签: ios objective-c uitableview