- (void)viewDidLoad
{
[super viewDidLoad];
selected = -1;
_key = [[NSMutableArray alloc]initWithObjects:@"我的好友",@"我的同事",@"我的网友", nil];
_value = [[NSArray alloc]initWithObjects:@"Alice",@"Tom",@"Edsion",nil];
_list = [[UITableView alloc]initWithFrame:CGRectMake(50, 100, 200, 50*_key.count) style:UITableViewStylePlain];
_list.dataSource = self;
_list.delegate = self;
_list.backgroundColor = [UIColor clearColor];
[self.view addSubview:_list];
_expandlist = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, 50*_value.count) style:UITableViewStylePlain];
_expandlist.dataSource = self;
_expandlist.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView==_list) {
return _key.count;
}else{
return _value.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* listcell = @"listcell";
static NSString* explancell = @"explancell";
static NSString* explan = @"explan";
UITableViewCell* cell = nil;
if (tableView==_list) {
if (selected==indexPath.row-1&&selected!=-1&&isTurn) {
cell = [tableView dequeueReusableCellWithIdentifier:explancell];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:explancell]autorelease];
CGRect frame = cell.frame;
frame.size.height = _expandlist.frame.size.height;
cell.frame = frame;
[cell addSubview:_expandlist];
}
[_expandlist reloadData];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:listcell];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listcell]autorelease];
cell.backgroundColor = [UIColor grayColor];
}
cell.textLabel.text = [_key objectAtIndex:indexPath.row];
}
}else{
cell = [tableView dequeueReusableCellWithIdentifier:explan];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:explan]autorelease];
}
cell.textLabel.text = [_value objectAtIndex:indexPath.row];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView==_list) {
if (selected==indexPath.row-1&&selected!=-1&&isTurn) {
return _expandlist.frame.size.height;
}
}
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView==_list) {
if (selected!=indexPath.row&&selected!=-1) {
if (isTurn) {
[_key removeObject:@"insert"];
[tableView reloadData];
int tem = 0;
if (selected<indexPath.row) {
tem = indexPath.row;
}else tem = indexPath.row+1;
selected = tem-1;
[_key insertObject:@"insert" atIndex:tem];
[_list insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:tem
inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}else{
selected = indexPath.row;
isTurn = !isTurn;
[_key insertObject:@"insert" atIndex:indexPath.row+1];
[_list insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}
}else{
selected = indexPath.row;
isTurn = !isTurn;
if (isTurn) {
[_key insertObject:@"insert" atIndex:indexPath.row+1];
[_list insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}else{
[_key removeObject:@"insert"];
[_list deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}
}
if (isTurn) {
CGRect frame = _expandlist.frame;
frame.size.height = _value.count*50;
_expandlist.frame = frame;
CGRect listframe = _list.frame;
listframe.size.height = frame.size.height+_list.frame.size.height;
_list.frame = listframe;
}
else{
CGRect frame = _list.frame;
frame.size.height = _key.count*50;
_list.frame = frame;
}
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:selected inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView==_list) {
cell.backgroundColor = [UIColor grayColor];
}
{
[super viewDidLoad];
selected = -1;
_key = [[NSMutableArray alloc]initWithObjects:@"我的好友",@"我的同事",@"我的网友", nil];
_value = [[NSArray alloc]initWithObjects:@"Alice",@"Tom",@"Edsion",nil];
_list = [[UITableView alloc]initWithFrame:CGRectMake(50, 100, 200, 50*_key.count) style:UITableViewStylePlain];
_list.dataSource = self;
_list.delegate = self;
_list.backgroundColor = [UIColor clearColor];
[self.view addSubview:_list];
_expandlist = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, 50*_value.count) style:UITableViewStylePlain];
_expandlist.dataSource = self;
_expandlist.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView==_list) {
return _key.count;
}else{
return _value.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* listcell = @"listcell";
static NSString* explancell = @"explancell";
static NSString* explan = @"explan";
UITableViewCell* cell = nil;
if (tableView==_list) {
if (selected==indexPath.row-1&&selected!=-1&&isTurn) {
cell = [tableView dequeueReusableCellWithIdentifier:explancell];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:explancell]autorelease];
CGRect frame = cell.frame;
frame.size.height = _expandlist.frame.size.height;
cell.frame = frame;
[cell addSubview:_expandlist];
}
[_expandlist reloadData];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:listcell];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listcell]autorelease];
cell.backgroundColor = [UIColor grayColor];
}
cell.textLabel.text = [_key objectAtIndex:indexPath.row];
}
}else{
cell = [tableView dequeueReusableCellWithIdentifier:explan];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:explan]autorelease];
}
cell.textLabel.text = [_value objectAtIndex:indexPath.row];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView==_list) {
if (selected==indexPath.row-1&&selected!=-1&&isTurn) {
return _expandlist.frame.size.height;
}
}
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView==_list) {
if (selected!=indexPath.row&&selected!=-1) {
if (isTurn) {
[_key removeObject:@"insert"];
[tableView reloadData];
int tem = 0;
if (selected<indexPath.row) {
tem = indexPath.row;
}else tem = indexPath.row+1;
selected = tem-1;
[_key insertObject:@"insert" atIndex:tem];
[_list insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:tem
inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}else{
selected = indexPath.row;
isTurn = !isTurn;
[_key insertObject:@"insert" atIndex:indexPath.row+1];
[_list insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}
}else{
selected = indexPath.row;
isTurn = !isTurn;
if (isTurn) {
[_key insertObject:@"insert" atIndex:indexPath.row+1];
[_list insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}else{
[_key removeObject:@"insert"];
[_list deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}
}
if (isTurn) {
CGRect frame = _expandlist.frame;
frame.size.height = _value.count*50;
_expandlist.frame = frame;
CGRect listframe = _list.frame;
listframe.size.height = frame.size.height+_list.frame.size.height;
_list.frame = listframe;
}
else{
CGRect frame = _list.frame;
frame.size.height = _key.count*50;
_list.frame = frame;
}
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:selected inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView==_list) {
cell.backgroundColor = [UIColor grayColor];
}
}