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. mysql/mariadb 数据库配置

    1.  启动mariadb systemctl start mariadb 2. 设置开机启动mariadb systemctl enable mariadb 一.修改用户密码,以root为例 1. ...

  2. 开例外!微信小程序登录绕过CAS单点登录(SSO)认证检查

    1 为了让微信API能够绕过CAS认证检查,将微信api入口部分设计为独立的模块.放入controller目录下,命名为wechat.java文件为WechatController.java 文件大体 ...

  3. JS原型与原型链终极讲解

    function Person () { this.name = 'John'; } var person = new Person(); Person.prototype.say = functio ...

  4. .NET编译过程

    总结一下.NET的编译过程, 一般的高级编程语言会把代码编译成机器码,也就是我们说的非托管代码,执行在编译它的电脑上. 而.NET编译代码的时候会把高级编程语言编译成中间语言 运行在CLR(公共语言运 ...

  5. Leetcode 计划

    如何正确高效地使用LeetCode? LeetCode按照怎样的顺序来刷题比较好? LeetCode 题目总结/分类 Leetcode 简略题解 - 共567题 500. Keyboard Row [ ...

  6. HDU2612(KB1-N)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 列表 enumerat 解包, 针对索引和元素

    dic = [1,2,3,4,5] for a,b in enumerate(dic): print(a,b) # a就是索引 b是元素

  8. 拜拜了,浮动布局-基于display:inline-block的列表布局——张鑫旭

    一.一抹前言 没有爱的日子,时间如指尖细沙,不知不觉就流逝了.写“CSS float浮动的深入研究.详解及拓展(一)”和“CSS float浮动的深入研究.详解及拓展(二)”似乎就在不久前,然而相隔差 ...

  9. 关于Datagridview控件用法的一些总结

    一.引言 Datagridview控件在winform开发中还是比较常用,一般的数据库系统都会使用它,但是想要友好的展示数据,形成良好的用户界面,那么就要对c#库中默认的Datagridview设置进 ...

  10. 葡萄城报表介绍:Java 报表

    一.Java 报表定义 Java 是一门面向对象编程语言,不仅吸收了 C++ 语言的各种优点,还摒弃了 C++ 里难以理解的多继承.指针等概念,因此 Java 语言具有功能强大和简单易用两个特征.Ja ...