自定义ExpandList效果

- (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];
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值