动态展开tableView的cell[1]

动态展开tableView的cell[1]

源码地址:https://github.com/xerxes235/HVTableView

虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-!!,不信,你可以去下载他的demo试一下:)

本人运行时的效果如下图:

源码:

RootViewController.m

//
//  RootViewController.m
//  AnimationTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "HVTableView.h"
#import "YXCell.h"

@interface RootViewController ()<HVTableViewDelegate, HVTableViewDataSource>

@property (nonatomic, strong) HVTableView *showTable;
@property (nonatomic, strong) NSArray     *picAry;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 图片源
    _picAry = @[[UIImage imageNamed:@"1.jpg"],
                [UIImage imageNamed:@"2.jpg"],
                [UIImage imageNamed:@"3.jpg"],
                [UIImage imageNamed:@"4.jpg"],
                [UIImage imageNamed:@"5.jpg"],
                [UIImage imageNamed:@"6.jpg"],
                [UIImage imageNamed:@"7.jpg"],
                [UIImage imageNamed:@"8.jpg"],
                [UIImage imageNamed:@"9.jpg"],
                [UIImage imageNamed:@"10.jpg"]];

    // tableView
    _showTable = \
        [[HVTableView alloc] initWithFrame:self.view.bounds
                         expandOnlyOneCell:YES
                          enableAutoScroll:YES];
    _showTable.HVTableViewDelegate   = self;
    _showTable.HVTableViewDataSource = self;
    [_showTable reloadData];

    // 加载进视图
    [self.view addSubview:_showTable];
}

#pragma mark - 各个代理

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _picAry.count;
}

//==============================================
#pragma mark 展开时的cell(可以添加动画)
//==============================================
-(void)tableView:(UITableView *)tableView
      expandCell:(UITableViewCell *)cell
   withIndexPath:(NSIndexPath *)indexPath
{
    YXCell *tmpCell = (YXCell *)cell;
    [UIView animateWithDuration:0.3f animations:^{
        tmpCell.showImageView.frame = CGRectMake(120, 0, 200, 200);
    }];

    [UIView animateWithDuration:0.5f animations:^{
        tmpCell.name.frame = CGRectMake(10, 10, 120, 20);
    }];
}

//==============================================
#pragma mark 收缩时的cell(可以添加动画)
//==============================================
-(void)tableView:(UITableView *)tableView
    collapseCell:(UITableViewCell *)cell
   withIndexPath:(NSIndexPath *)indexPath
{
    YXCell *tmpCell = (YXCell *)cell;
    [UIView animateWithDuration:0.3f animations:^{
        tmpCell.showImageView.frame = CGRectMake(0, 0, 100, 100);
        tmpCell.name.frame = CGRectMake(10, 300, 120, 20);
    }];
}

//==============================================
#pragma mark 返回高度
//==============================================
-(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
         isExpanded:(BOOL)isexpanded
{
    if (isexpanded == YES)
    {
        // 展开时的高度
        return 200;
    }
    else
    {
        // 没有展开时的高度
        return 100;
    }
}

//==============================================
#pragma mark 返回高度
//==============================================
-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
                   isExpanded:(BOOL)isExpanded
{
    static NSString *CellIdentifier = @"aCell";
    YXCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[YXCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                             reuseIdentifier:CellIdentifier];
    }

    // 选择时没有颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    // 加载图片
    cell.showImageView.image = _picAry[indexPath.row];

    // 没有展开cell,进行一些设置(不要添加任何动画)
    if (isExpanded == NO)
    {
        cell.showImageView.frame = CGRectMake(0, 0, 100, 100);
        cell.name.frame          = CGRectMake(10, 300, 120, 20);
    }
    // 展开的cell,进行一些设置(不要添加任何动画)
    else
    {
        cell.showImageView.frame = CGRectMake(120, 0, 200, 200);
        cell.name.frame          = CGRectMake(10, 10, 120, 20);
    }

    return cell;
}

@end

YXCell.h
//
//  YXCell.h
//  AnimationTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YXCell : UITableViewCell

@property (nonatomic, strong) UILabel     *name;
@property (nonatomic, strong) UIImageView *showImageView;

@end

YXCell.m
//
//  YXCell.m
//  AnimationTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "YXCell.h"

@implementation YXCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        // 尺寸在外面的cell设定
        _showImageView  = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:_showImageView];

        _name           = [[UILabel alloc] initWithFrame:CGRectZero];
        _name.font      = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:17.f];
        _name.text      = @"YouXianMing";
        _name.textColor = [UIColor blackColor];
        [self addSubview:_name];

        self.layer.masksToBounds = YES;
    }
    return self;
}

@end

关键的几步:

怎么实现复杂逼格高的动画?这个就需要你的想象力来脑补了-_-!!,没有实现不出来的效果,只有想不出来的效果:)

 

附录:

其实笔者深刻理解他的原理,然后尝试着自己写了一个,不过,那恶心的重用问题,不自己亲自动手是不理解别人写代码的用心良苦的-_-!!!!!

先共享源码供君尝试:

//
//  YXCell.h
//  ExpendTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YXCell : UITableViewCell

@property (nonatomic, strong) UIView *showView;

@end
//
//  YXCell.m
//  ExpendTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "YXCell.h"

@implementation YXCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _showView = [[UIView alloc] initWithFrame:CGRectZero];
        _showView.backgroundColor = [UIColor redColor];
        [self addSubview:_showView];
    }
    return self;
}

@end
//
//  RootViewController.m
//  ExpendTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXCell.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

{

    BOOL  flag[10];

}

@property (nonatomic, strong) UITableView      *tableView;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                              style:UITableViewStylePlain];
    _tableView.delegate   = self;
    _tableView.dataSource = self;

    [self.view addSubview:_tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (flag[indexPath.row] == YES)
    {
        return 200.f;
    }
    else
    {
        return 70.f;
    }
}

//==============================================
#pragma mark  根据cell状态进行相关设置
//==============================================
-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"aCell";
    YXCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[YXCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                             reuseIdentifier:CellIdentifier];
    }

    // 选择时没有颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (flag[indexPath.row] == YES)
    {
        [UIView animateWithDuration:1.f animations:^{
            cell.showView.frame = CGRectMake(0, 0, 200, 50);
        }];
    }
    else
    {
        cell.showView.frame = CGRectMake(-200, 0, 200, 50);
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (flag[indexPath.row] == NO)
    {
        for (int i = 0; i < 10; i++)
        {
            flag[i] = NO;
        }

        flag[indexPath.row] = YES;

        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView endUpdates];
    }
    else
    {
        flag[indexPath.row] = NO;

        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView endUpdates];
    }
}

@end

以下三个地方是相互配合的,但还是难以解决重用问题-_-!!!!

 

 

时间: 2024-09-02 01:44:18

动态展开tableView的cell[1]的相关文章

动态展开tableView的cell[2]

动态展开tableView的cell[2] http://code4app.com/ios/%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0cell/53845f8a933bf0740a8b458a 这份代码也是参考别人而写的-_-! 效果: 其实呢,这份代码本人是不推荐的,很难维护,因为他的原理就是添加删除cell,会有这复杂的删除添加逻辑. 源码: // // RootViewController.m // InsertTableViewCell // // Copy

使用HVTableView动态展开tableView中的cell

使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTableView // // Created by Hamidreza Vakilian on 25/11/2013 // Copyright (c) 2013 Hamidreza Vakilian. All rights reserved. // Website: http://www.infracy

动态切换tableView中的cell的种类

动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直接继承系统的就行了. // // RootViewController.m // ChangeCell // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import

解决tableView中cell动态加载控件的重用问题

解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问题,即使你能做到该cell只根据数值加载了一回控件,你也没法保证不出现重用问题:) 效果(请注意查看,移动下面的格子时,上面出现了重用的问题) 源码: YXCell.h // // YXCell.h // YXTableView // // Copyright (c) 2014年 Y.X. All

ios-iOS中点击tableview的cell没有调用代理方法didSelectRow

问题描述 iOS中点击tableview的cell没有调用代理方法didSelectRow 我在控制器中创建一个tableview也给这个控制器设置了代理和数据源可是点击cell没有调用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 解决方案 这个方法是UITableView的代理方法,你可以将tableView的delegate设置为控制器,然后在cotro

tableview的cell问题-关于tableview的cell的问题

问题描述 关于tableview的cell的问题 我自定义的cell,给每一个cell上都添加了一个UIPanGestureRecognizer手势,但是我现在想让其中一个cell触发手势的时候别的cell手势都禁用,也就是两个手指拖动两个cell是没有效果的,我该如何判断呢? 我判断了 if (!(gesture.maximumNumberOfTouches = 1)) { return; } 似乎没有效果啊..... 求在线解答 解决方案 关于- tableView:cellForRowAt

TableView的cell加载倒计时重用问题解决方案

TableView的cell加载倒计时重用问题解决方案 效果     说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效完美的解决此类倒计时重用问题   源码 https://github.com/YouXianMing/CountDownTimerForTableView   细节  

ios-怎么动态生成tableview?

问题描述 怎么动态生成tableview? 我现在要实现一个功能是 我打开一个页面后,提交一个请求到后端, 从后端取得数据 再在客户端显示出来, 我用tableview 来显示. 怎么样根据后端返回的数据来动态生成tableview? 解决方案 通过afnetworking 获取数据,成功获取数据后 tableview 执行reload 函数 解决方案二: 先设计好UITableViewCell的样式,根据返回的数据,拼装NSMuTableArray,然后加载到UITableView中 解决方案

更新tableView的某个cell

更新tableView的某个cell 异步加载完数据后更新某个cell,这应该是非常常见的使用方法了,我们经常会用reloadData. 效果: 源码: // // RootViewController.m // DataTableView // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import "SDWebImage.h" @inte