zl程序教程

您现在的位置是:首页 >  其它

当前栏目

动态展开tableView的cell[1]

动态 展开 cell tableView
2023-09-14 08:57:17 时间
-(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;

@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 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

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

 

 




动态计算UITableviewcell高度 在iOS开发中,我们少不了和UITableview打交道,因为UITableview也是UIKit中最复杂的一个控件了。在使用UITableview的过程中,UITableviewCell也是必不可少的,页面列表形式的展示可谓是各种各样,相信不少童鞋们也曾为复杂的页面布局困惑过,其中比较难的也就数cell的高度自适应了,也就是说cell的高度是根据内容来动态计算的。