展开和折叠桌面单元格
问题描述:
嘿,我是新手在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];
}
}
}
和我的最小化等作为
-(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并在第二次单击Expandable单元时将其折叠。
但我想这样工作:点击任何可展开的单元格,其他可展开的单元格将自动崩溃。
任何人都可以帮助我吗?
在此先感谢。
答
当调用didSelectRowAtIndexPath:
之后,可以将最后一个可展开的单元格保存为折叠状态。
您@interface
后放:
@property (nonatomic, strong) NSIndexPath *lastIndexPath;
现在你可以在你的didSelectRowAtIndexPath
喜欢使用它:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Collapse the last cell expanded
if (indexPath.section != 0) {
if(self.lastIndexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
NSDictionary *d=[self.arForTable objectAtIndex:self.lastIndexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
} [self miniMizeThisRows:ar];
}
}
// Save the current indexPath as the last one expanded
self.lastIndexPath = indexPath;
// This is your current code - I didn't change it
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];
}
}
}
也许代码需要一些ajustments,而是看它是否能帮助你。