- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//当cell被选择(被点击)时调用的函数
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TableCell *cell=[_TableArry objectAtIndex:indexPath.row];
NSLog(@"%d",indexPath.row);
if(cell.ChildArray.count==0)//如果没有子菜单
{
NSLog(@"要打开页面");
}
else
{
if(!cell.Open)//如果子菜单是关闭的
{
NSArray * array = [self insertOperation:cell];
if(array.count>0)
//从视图中添加
[self.TableView insertRowsAtIndexPaths: array withRowAnimation:UITableViewRowAnimationBottom ];
}
else//如果子菜单是打开的
{
NSArray * array = [self deleteOperation:cell];
if(array.count>0)
//从视图中删除
[self.TableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationBottom];
}
}
}
-(NSArray *) insertOperation:(TableCell *)item
{
[_InsertArry removeAllObjects];//将插入菜单清空
NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:item] inSection:0];//获取选取的cell的位置
NSLog(@"长度为%d",path.row);
TableCell *child = [[TableCell alloc]init];
//遍历当前选取cell 的子菜单
for(int i=0;i<item.ChildArray.count;i++)
{
child = [item.ChildArray objectAtIndex:i];
[_TableArry insertObject:child atIndex:path.row + i +1 ];//调用数组函数将其插入其中
[_InsertArry addObject:child];//放入插入数组中
}
item.Open=YES;//设置菜单已经打开
NSMutableArray *PathArray= [NSMutableArray array];//初始化用于存放位置的数组
for(TableCell * cell in _InsertArry)
{
NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:cell] inSection:0];
[PathArray addObject:path];
}
return PathArray;
}
-(NSArray *) deleteOperation:(TableCell *)item
{
[_DeleteArry removeAllObjects];//清空删除数组
TableCell *child =[[TableCell alloc]init];//子菜单
for(int i =0;i<item.ChildArray.count;i++)
{
child = [item.ChildArray objectAtIndex:i];
[_DeleteArry addObject:child];//添加到删除数组
}
item.Open = NO;//设置子视图关闭
NSMutableArray *mutableArry = [NSMutableArray array];
NSMutableIndexSet *set= [NSMutableIndexSet indexSet];//设置用来存放删除的cell的索引
for(TableCell *cell in _DeleteArry)
{
NSIndexPath *path = [NSIndexPath indexPathForRow:[_TableArry indexOfObject:cell] inSection:0];
NSLog(@"%d",path.row);
[mutableArry addObject:path];
[set addIndex:path.row];
}
[_TableArry removeObjectsAtIndexes:set];//调用函数来从数组中删除
return mutableArry;
}
@end
这个主要是参考csdn上下载的一个二级菜单的例子;
但是有些不一样,如果他的代码你看不懂,把我的看懂了再去看他的就简单了;
可以下载我的源码运行看一下;
------------------------------------------分割线------------------------------------------
具体下载目录在 /2014年资料/8月/25日/iOS 二级菜单(UITableView实现)
------------------------------------------分割线------------------------------------------