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 和 ...
随机推荐
- golang prometheus包的使用
prometheus包提供了用于实现监控代码的metric原型和用于注册metric的registry.子包(promhttp)允许通过HTTP来暴露注册的metric或将注册的metric推送到Pu ...
- MySQL命令行导入导出数据
参考:http://www.cnblogs.com/xcxc/archive/2013/01/30/2882840.html 这篇文章写得非常好,又简洁,而且深入浅出,排版也非常好看,不会像网上的只是 ...
- jquery ajax abort()方法
如果用户频繁点击ajax请求,除最后一个外都是无效的,趁早结束节省资源.也可能出现更严重的问题,最后一个发送的请求,响应未必是最后一个,有可能造成混乱.用jquery的abort方法,可以中途中止aj ...
- 使用 Selenium 实现基于 Web 的自动化测试
(转自http://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb/index.html) Selenium 是一个用于 Web 应 ...
- Hive集成HBase实践
#step1: create hive table 't_test' hive -e "create table test.t_user(id int,name string,age int ...
- PHP 集成开发环境比较
专注了这么些年技术,没有养成记录和积累的习惯.如今乐于开源和分享经验,却停笔踌躇,不知该从何处说起.开通博客也有一段时间了,也没能写出一篇像样的文章,其实这篇文章也是被我拉壮丁似的用来练手的.思前想后 ...
- asp后台读id设置样式
加runat=“server”
- Oracle安装后遇到错误:The Network Adapter could not establish the connection
http://note.youdao.com/noteshare?id=e6baee7ea7b7f60d7a265124e2bdd46c&sub=988945C6DDE843D5A7D6588 ...
- Android全屏的两种方法
在开发中我们经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方法是在配置文件里改! 一.在代码中设置: [java] view plain copy package ...
- 【转】分布式环境下5种session处理策略(大型网站技术架构:核心原理与案例分析 里面的方案)
前言 在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理.如果不做任何处理的话,用户将出现频繁登录的现象,比如集群中存在A.B两台服务器,用户在第一次访问网站时,Ngin ...