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 和 ...
随机推荐
- mysql 导出数据到csv文件的命令
1.导出本地数据库数据到本地文件 mysql -A service_db -h your_host -utest -ptest mysql> select * from t_apps where ...
- 短视频APP是如何开启你的美好生活的?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯视频云终端团队发表于云+社区专栏 常青, 2008 年毕业加入腾讯,一直从事客户端研发相关工作,先后参与过 PC QQ.手机QQ. ...
- Maven 映像
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- ruby执行周期性任务
1.前言 无论是用ruby做系统管理,还是用rails做web开发,都可能遇到周期性任务,它们按照一定时间周期(1小时,2天......)持续地触发.在ruby中,我认为一次性任务使用sid ...
- golang基础--控制语句
go基础之控制语句 补充知识 指针 与其他语言不同,在Go中不支持指针运算即->运算符,而直接采用.选择符来操作指针目标对象的成员. 操作符&取变量的地址,使用*通过指针间间接访问目标对 ...
- windows server服务器上部署java+tomcat网站域名配置
如果只是部署java项目的话,可以把IIS删除,然后在服务器上安装jdk tomcat 配置好环境变量,就和你在自己计算机上开发一样,把你的项目war包拷到tomcat下的webapps里(任意目录都 ...
- Python jieba库的使用说明
1.jieba库基本介绍 (1).jieba库概述 jieba是优秀的中文分词第三方库 - 中文文本需要通过分词获得单个的词语 - jieba是优秀的中文分词第三方库,需要额外安装 - ...
- jquery 关于使用 append 追加 元素后 事件无法触发
当在使用js或jQuery创建元素时,用 on(事件,function(){代码}) 或者 事件(function(){代码 })绑定事件时 在使用append添加元素后 由于是在页面加载完成之后进行 ...
- JavaScript push()函数追加数组数据
将数据追加到一个数组末尾的最简单的方法是通过 push() 函数. .push() 允许有一个或多个参数,并把它“push”到数组的末尾. var arr = [1,2,3];arr.push(4); ...
- google自定义广告系列
Part1:说明 向网址添加参数以标识引荐流量的广告系列. 通过向在广告系列中使用的目标网址添加广告系列参数,您可以收集这些广告系列整体效果的相关信息,还可以了解广告系列在何处投放时效果更好.例如,您 ...