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时崩溃的处理的更多相关文章

  1. 动画删除cell出问题

    删除UITableView行的代理时出了问题 解决办法 先remove数据,再执行 [_mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObje ...

  2. RumTime实践之--UITableView和UICollectionView缺省页的实现

    有关RunTime的知识点已经看过很久了,但是一直苦于在项目中没有好的机会进行实际运用,俗话说"光说不练假把式",正好最近在项目中碰到一个UITableView和UICollect ...

  3. UITableView 自带编辑删除 自己定义button

    一:UITableView 自带编辑删除 1:实现两个方法就可以 #pragma mark   tableView自带的编辑功能 -(void)tableView:(UITableView *)tab ...

  4. UITableView和UICollectionView的Cell高度的几种设置方式

    UITableViewCell 1.UITableView的Cell高度默认由rowHeight属性指定一个低优先级的隐式约束 2.XIB中可向UITableViewCell的contentView添 ...

  5. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  6. 复习知识点:UITableView和UICollectionView的常用属性

    UITableView UICollectionView  //UICollectionViewLayout //UICollectionViewLayout决定了UICollectionView如何 ...

  7. [转]iOS8 自动调整UITableView和UICollectionView布局

    转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog)   本文讲述了UITableView.UICollec ...

  8. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  9. iOS全埋点解决方案-UITableView和UICollectionView点击事件

    前言 在 $AppClick 事件采集中,还有两个比较特殊的控件: UITableView •UICollectionView 这两个控件的点击事件,一般指的是点击 UITableViewCell 和 ...

随机推荐

  1. nginx 学习笔记(2) nginx新手入门

    这篇手册简单介绍了nginx,并提供了一些可以操作的简单的工作.前提是nginx已经被安装到你的服务器上.如果没有安装,请阅读上篇:nginx 学习笔记(1) nginx安装.这篇手册主要内容:1. ...

  2. Spring配置Quartz任务调度、及 ThreadPool 线程池

    ONE.除了引入 Spring 相关的 jar 包,还要引入 Quartz 的 jar 包 <dependency> <groupId>org.springframework& ...

  3. Linux中Redis的安装

    一.下载redis redis官网地址:http://www.redis.io/ 下载地址:http://download.redis.io/releases/ redis中文文档地址:http:// ...

  4. .net WINFORM的GDI双缓冲的实现

    有时候在窗体中执行不断的GDI+操作的时候会出现闪速的状况,除了修改窗体的参数,更应该解决刷新本身的问题,双缓冲可能就是这样来的. 方法1: 用GDI绘制在位图上,然后再重新生成位图 Bitmap b ...

  5. 19、网络编程 (Socket套接字编程)

    网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用层.传输层.网络层和链路层,每层分别负责不同的通信功能,接下来针对这四层进行详细地讲解. 链路层:链路层是用于定义物理传输通道,通常是对某些 ...

  6. 2 springboot多模块项目

    一般来说创建一个springboot工程基本就可以了,但是有的时候可能需要将业务模块逻辑划分,每块业务模块都是一个工程,下边演示下多模块进行开发 目录结构 ...somefun ......somef ...

  7. Linux常用指令大全

    2017-03-25   16:35:42 刚开始学习Linux,由于记忆力有限,把平时常用的Linux命令整理出来,以便随时查阅:  linux 基本命令    ls     (list 显示当前目 ...

  8. 新项目找不到Angular-cli.json文件

    新项目找不到Angular-cli.json文件 Angular-cli.json文件是Angular5中cli的相关配置信息. 今天创建了新Angular项目后突然发现Angular-cli.jso ...

  9. BZOJ4977: [[Lydsy1708月赛]跳伞求生

    传送门 直接贪心 考虑到 \(n\) 个人的贡献都是 \(a_i\),另外 \(m\) 个人的贡献都是 \(c_i-b_i\) 首先 \(a_i>b_j\) 的限制不好做,所以将 \(a,b\) ...

  10. JS全局变量VAR和THIS--在函数内部,加var是局部变量,不加是全局变量

    JS全局变量VAR和THIS 2011-05-23 21:43 很多人都觉得在JavaScript声明一个变量,加var和不加var没有什么区别,实际上是一个错误的观点,如果在函数外面,也就是说在wi ...