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:self
action:@selector(buttonsEvent:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button]; _title = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_title.textAlignment = NSTextAlignmentLeft;
_title.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.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.bounds
style: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的indexPath
NSIndexPath *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 = .f; UIButton *button = [[UIButton alloc] initWithFrame:self.bounds];
[button addTarget:self
action:@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 / .f, ); // 单元格尺寸
self.sectionInset = UIEdgeInsetsMake(, , , ); // 单元格边缘
self.minimumInteritemSpacing = ; // 横排单元格最小间隔
self.minimumLineSpacing = ; // 单元格最小行间距
}
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.bounds
collectionViewLayout:[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的indexPath
NSIndexPath *indexPath = [_collectionView indexPathForCell:cell]; // 删除数据源
[_dataArray removeObjectAtIndex:indexPath.row]; // 执行删除动画效果
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];
} @end
分析:


注意:
1. 先取得cell的indexPath
2. 删除数据源
3. 执行删除cell的操作,带动画
执行delete操作的时候,并不会刷新数据源,不会执行reloadData,注意.
UITableVIew与UICollectionView带动画删除cell时崩溃的处理的更多相关文章
- 动画删除cell出问题
删除UITableView行的代理时出了问题 解决办法 先remove数据,再执行 [_mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObje ...
- RumTime实践之--UITableView和UICollectionView缺省页的实现
有关RunTime的知识点已经看过很久了,但是一直苦于在项目中没有好的机会进行实际运用,俗话说"光说不练假把式",正好最近在项目中碰到一个UITableView和UICollect ...
- UITableView 自带编辑删除 自己定义button
一:UITableView 自带编辑删除 1:实现两个方法就可以 #pragma mark tableView自带的编辑功能 -(void)tableView:(UITableView *)tab ...
- UITableView和UICollectionView的Cell高度的几种设置方式
UITableViewCell 1.UITableView的Cell高度默认由rowHeight属性指定一个低优先级的隐式约束 2.XIB中可向UITableViewCell的contentView添 ...
- iOS 8自动调整UITableView和UICollectionView布局
本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...
- 复习知识点:UITableView和UICollectionView的常用属性
UITableView UICollectionView //UICollectionViewLayout //UICollectionViewLayout决定了UICollectionView如何 ...
- [转]iOS8 自动调整UITableView和UICollectionView布局
转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog) 本文讲述了UITableView.UICollec ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- iOS全埋点解决方案-UITableView和UICollectionView点击事件
前言 在 $AppClick 事件采集中,还有两个比较特殊的控件: UITableView •UICollectionView 这两个控件的点击事件,一般指的是点击 UITableViewCell 和 ...
随机推荐
- spring中获取applicationContext
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...
- java 集合框架小结
一:集合框架 集合框架是为表示和操作集合而规定的一种统一的标准的体系结构. 任何集合框架都包含三大块内容:对外的接口.接口的实现和对集合运算的算法. 接口:即表示集合的抽象数据类型.Colle ...
- nginx 学习笔记(5) nginx调试日志
为启动一个调试日志,nginx需要在构建时配置城支持调试模式. ./configure --with-debug ... 而且调试级别应该使用err_log指令来设置: err_log /path/t ...
- 【LeetCode题解】142_环形链表2(Linked-List-Cycle-II)
目录 描述 解法一:哈希表 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针 思路 Java 实现 Python 实现 复杂度分析 描述 给定一个链表,返回链表开始入环的第一个节点 ...
- ClickOnce 发布WinForm应用程序(非签名方式)
ClickOnce IIS7发布WinForm应用程序,非签名方式(不勾选签名中的"为ClickOnce清单签名") 一.在D盘上建一个文件夹”MyAppPath”. 该 ...
- (一)面向对象的javascript
javascript是一门典型的动态类语言 一:鸭式辨型(指导我们关注对象的行为,而不关注对象本身). var duck = { duckString: function(){ console.log ...
- SQL语句的增删改查(详细)--转载
转载源: http://blog.csdn.net/a88055517/article/details/6736284/ 一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [ ...
- 【原】Maven解决Jar包冲突
一.起源 引入二方jar maven 包后出现 NoSuchMethodError org.apache.commons.lang3.StringUtils.isNoneEmpty . 第一感觉就是j ...
- Java代理(三)
前面说到了java的动态代理,但是动态代理依赖于接口,这次来看看cglib来实现的代理... 假设有如下方法,这回没有说接口哦~ package proxy.cglibProxy; public cl ...
- mybatis之使用注解
注解 使用对象 相对应的 XML 描述 @CacheNamespace 类 <cache> 为给定的命名空间(比如类)配置缓存.属性有:implemetation, eviction, f ...