UITableVIew与UICollectionView带动画删除cell时崩溃的处理
UITableVIew与UICollectionView带动画删除cell时崩溃的处理
-会崩溃的原因是因为没有处理好数据源与cell之间的协调关系-
效果:
tableView的源码:
ModelCell.h + ModelCell.m
// // ModelCell.h // Set // // Created by YouXianMing on 14/11/24. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @class ModelCell;@protocol ModelCellDelegate <NSObject> @optional - (void)modelCellButton:(ModelCell *)cell; @end@interface ModelCell : UITableViewCell@property (nonatomic, weak) id<ModelCellDelegate> delegate;@property (nonatomic, strong) UILabel *title;@end
// // ModelCell.m // Set // // Created by YouXianMing on 14/11/24. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ModelCell.h"@implementation ModelCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {UIButton *button = [[UIButton alloc] initWithFrame:self.bounds];[button addTarget:selfaction:@selector(buttonsEvent:)forControlEvents:UIControlEventTouchUpInside];[self addSubview:button];_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 44)];_title.textAlignment = NSTextAlignmentLeft;_title.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:17.f];[self addSubview:_title];}return self; }- (void)buttonsEvent:(UIButton *)button {if (_delegate && [_delegate respondsToSelector:@selector(modelCellButton:)]) {[_delegate modelCellButton:self];} }@end
控制器源码:
// // ViewController.m // Set // // Created by YouXianMing on 14/11/24. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "ModelCell.h"@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, ModelCellDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArray; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 初始化数据源_dataArray = [NSMutableArray array];[_dataArray addObject:@"YouXianMing"];[_dataArray addObject:@"Job"];[_dataArray addObject:@"NoZuoNoDie"];[_dataArray addObject:@"XiaoMing"];[_dataArray addObject:@"Smith"];[_dataArray addObject:@"K.K.K."];// 初始化tableView_tableView = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];_tableView.delegate = self;_tableView.dataSource = self;_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;[self.view addSubview:_tableView];[_tableView registerClass:[ModelCell class] forCellReuseIdentifier:@"YouXianMing"]; }#pragma mark - 代理 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [_dataArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YouXianMing"];cell.delegate = self;cell.title.text = _dataArray[indexPath.row];return cell; }- (void)modelCellButton:(ModelCell *)cell {// 获取到cell的indexPathNSIndexPath *indexPath = [_tableView indexPathForCell:cell];// 删除数据源 [_dataArray removeObjectAtIndex:indexPath.row];// 执行删除动画效果 [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }@end
UICollectionView源码:
ModelCell.h + ModelCell.m
// // ModelCell.h // collection // // Created by YouXianMing on 14/11/25. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @class ModelCell;@protocol ModelCellDelegate <NSObject> @optional - (void)modelCellEvent:(ModelCell *)cell; @end@interface ModelCell : UICollectionViewCell@property (nonatomic, weak) id<ModelCellDelegate> delegate; @property (nonatomic, strong) UILabel *title;@end
// // ModelCell.m // collection // // Created by YouXianMing on 14/11/25. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ModelCell.h"@implementation ModelCell- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {_title = [[UILabel alloc] initWithFrame:self.bounds];_title.textAlignment = NSTextAlignmentCenter;[self addSubview:_title];self.layer.borderWidth = 1.f;UIButton *button = [[UIButton alloc] initWithFrame:self.bounds];[button addTarget:selfaction:@selector(buttonEvent:)forControlEvents:UIControlEventTouchUpInside];[self addSubview:button];}return self; }- (void)buttonEvent:(UIButton *)button {if (_delegate && [_delegate respondsToSelector:@selector(modelCellEvent:)]) {[_delegate modelCellEvent:self];} }@end
CellLayout.h + CellLayout.m
// // CellLayout.h // collection // // Created by YouXianMing on 14/11/25. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h>@interface CellLayout : UICollectionViewFlowLayout@end
// // CellLayout.m // collection // // Created by YouXianMing on 14/11/25. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "CellLayout.h"@implementation CellLayout- (instancetype)init {self = [super init];if (self) {self.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 3.f, 140); // 单元格尺寸self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 单元格边缘self.minimumInteritemSpacing = 0; // 横排单元格最小间隔self.minimumLineSpacing = 0; // 单元格最小行间距 }return self; }@end
控制器源码:
// // ViewController.m // collection // // Created by YouXianMing on 14/11/25. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "CellLayout.h" #import "ModelCell.h"@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, ModelCellDelegate> @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataArray; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 初始化数据源_dataArray = [NSMutableArray array];[_dataArray addObject:@"YouXianMing"];[_dataArray addObject:@"Job"];[_dataArray addObject:@"NoZuoNoDie"];[_dataArray addObject:@"XiaoMing"];[_dataArray addObject:@"Smith"];[_dataArray addObject:@"K.K.K."];// 创建出UICollectionView_collectionView = [[UICollectionView alloc] initWithFrame:self.view.boundscollectionViewLayout:[CellLayout new]];_collectionView.backgroundColor = [UIColor whiteColor];_collectionView.delegate = self;_collectionView.dataSource = self;[_collectionView registerClass:[ModelCell class] forCellWithReuseIdentifier:@"YouXianMing"];[self.view addSubview:_collectionView]; }#pragma mark - 代理 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return [_dataArray count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {ModelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YouXianMing"forIndexPath:indexPath];cell.title.text = _dataArray[indexPath.row];cell.delegate = self;return cell; } - (void)modelCellEvent:(ModelCell *)cell {// 获取到cell的indexPathNSIndexPath *indexPath = [_collectionView indexPathForCell:cell];// 删除数据源 [_dataArray removeObjectAtIndex:indexPath.row];// 执行删除动画效果 [_collectionView deleteItemsAtIndexPaths:@[indexPath]]; }@end
分析:
注意:
1. 先取得cell的indexPath
2. 删除数据源
3. 执行删除cell的操作,带动画
执行delete操作的时候,并不会刷新数据源,不会执行reloadData,注意.
转载于:https://www.cnblogs.com/YouXianMing/p/4121774.html
UITableVIew与UICollectionView带动画删除cell时崩溃的处理相关推荐
- Xcode - 使用 DWURecyclingAlert 进行 UITableView 和 UICollectionView 的绘图性能测试https://viktyz.gitbooks.io/ios
源链接:https://viktyz.gitbooks.io/iosnotebook-gitbook/Notes/Note_00144_20160301.html 方案名称 Xcode - 使用 DW ...
- UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件...
一 .问题:你想用流畅直观的动画来移动和拖拽TableView中的cell和section 方案: 用moveSection:toSection:方法把一个Section移动到新位置. 用moveR ...
- UITableView 自带编辑删除 自己定义button
一:UITableView 自带编辑删除 1:实现两个方法就可以 #pragma mark tableView自带的编辑功能 -(void)tableView:(UITableView *)tab ...
- Android实现GridView的item长按拖动删除完美实现(带动画效果)
领导这几天让做一个项目,就是可以实现像支付宝首页一样的可以长按拖动,删除的界面,以前没做过,领导让我做的时候觉得简直是老虎吃天,无从下手啊,可是领导的任务还是要实现的,没办法,就自己网上找咯,但是网上 ...
- 复习知识点:UITableView和UICollectionView的常用属性
UITableView UICollectionView //UICollectionViewLayout //UICollectionViewLayout决定了UICollectionView如何 ...
- UE4(unreal engine4)蒙太奇动画删除不想要的帧数
UE4系列文章目录 文章目录 UE4系列文章目录 前言 一.问题原因 二.具体操作步骤 前言 UE4(unreal engine4)蒙太奇动画删除不想要的帧数.当我们在UE4中导入一个fbx骨骼动画. ...
- 仿支付宝输入支付密码2(带动画)
由于用对话框做输入支付密码会出现宽度无法充满,这次我用popwindow来做,功能效果更加容易扩展:这次提供源码噢,有兴趣的可以下载去参考下,带动画噢: 先看看效果图把: 主界面只有这么简单,就不多在 ...
- HTML5动态圆形导航,jQuery带动画特效的圆形导航菜单特效
这是一款jQuery带动画特效的圆形导航菜单特效.该导航菜单在被点击时,会以动画的方式移动到屏幕中间,并展开为一个圆形菜单,效果非常炫酷. 使用方法 在页面中引入jquery和TweenMax.js的 ...
- animate用法 js原生_用 原生Javascript 创建带动画的固顶导航菜单
当我们在网页中加入一个导航菜单的时候,需要考虑很多因素.如何确定它的位置?如何定义样式?还需要保证它具有良好的响应性.又或者你想为它添加一些炫酷的动画.这时你可能会对 jQuery 感兴趣,因为它会帮 ...
- 用TableView写带特效的cell
用TableView写带特效的cell 效果: 源码地址: https://github.com/YouXianMing/UI-Component-Collection 分析: 在UIScrollVi ...
最新文章
- Angela启动步骤
- 解决git pull/push每次都需要输入密码问题
- 在 Java EE 组件中使用 Camel Routes
- 58-混沌感悟.(2015.2.11)
- 共享变量 tensorflow解读
- Boost.Asio的网络编程
- 程序猿的奋斗史(三十八)——大学断代史(二)——我与数据库的故事
- HTML中合并单元格(水平和垂直)
- 基于3D技术的机器视觉解决方案
- 每天定投10元基金有意义吗?
- 计算指定位数的圆周率
- 2012,当我们谈论移动互联网创业时,我们在谈论些什么?
- 依赖背包dp NOIP2006 vijos 1313 金明的预算方案
- vue调用本地摄像头实现拍照
- Shell脚本交互:自动输入密码
- javascript获取本周、本月、本季度、本年时间
- memtester4.3.0
- 但是我喜欢计算机课用英语怎么说,语文课的英文
- hdu 2276【Kiki Little Kiki 2】
- SCI论文投稿信(Cover Letter)的写法及模版(转