collectionView的删除与添加

(1)目前我的需求是这样的:要在一个cell中显示一张图片和描述文字,以及CollectionView中有多个section。为了便于后续开发,所以我要先定义两个模型:Section模型和Cell模型。Section模型用来定义CollectionView中所有Section包含的属性,Cell模型用来定义一个Section中每一个Cell的属性。Model实现代码如下:
SectionModel.h:
#import@interface SectionModel : NSObject
//定义Section头的名字;
@property(nonatomic,copy) NSString *sectionName;
//定义Section中的cell数组;
@property(nonatomic,strong) NSMutableArray *cellArray;
//这里存放的是section中的每一个cell;
@end
CellModel.h:
#import@interface CellModel : NSObject
//定义cell中的图片;
@property(nonatomic,strong) NSString *cellImage;
//定义cell中的描述文字;
@property(nonatomic,strong) NSString *cellDesc;@end
(2)View的设计如下:由于要做cell的删除操作,所以我要在每一个cell上面放一个删除按钮。我这里cell是纯代码实现的,代码如下:

 CollectionViewCell.h:(m文件请参看源代码)#import@interface CollectionViewCell : UICollectionViewCell
//cell中的图片;
@property(strong,nonatomic) UIImageView *imageView;
//cell中的描述文本;
@property(strong,nonatomic) UILabel *descLabel;
//cell右上角的删除按钮;
@property(nonatomic,strong)UIButton *deleteButton;
@end

(3)核心代码实现:
页面的右上角会有一个编辑按钮,点击编辑按钮,cell就处于可删除状态。所以cell共有两种状态,正常状态和可删除状态,我用枚举来实现:

@property(nonatomic,assign) enum CellState;

NS_ENUM(NSInteger,CellState){
    //右上角编辑按钮的两种状态;
    //正常的状态,按钮显示“编辑”;
    NormalState,
    //正在删除时候的状态,按钮显示“完成”;
    DeleteState
};

(4)- (UICollectionViewCell *)collectionView: cellForItemAtIndexPath:的实现如下:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
//找到某一个具体的Section;
SectionModel *sec = [self.dataSectionArray objectAtIndex:indexPath.section];
//找到Section中的cell数组中某个具体的cell;
CellModel *cel = [sec.cellArray objectAtIndex:indexPath.row];
//取出数据;
cell.imageView.image = [UIImage imageNamed:cel.cellImage];
cell.descLabel.text = cel.cellDesc;
//设置删除按钮
// 点击编辑按钮触发事件
if(CellState == NormalState){
//正常情况下,所有删除按钮都隐藏;
cell.deleteButton.hidden = true;
}else {
//可删除情况下;
//找到某个具体的section;
SectionModel *section = self.dataSectionArray[indexPath.section];
//cell数组中的最后一个是添加按钮,不能删除;
if (indexPath.row == section.cellArray.count - 1){
cell.deleteButton.hidden = true;
}else {
cell.deleteButton.hidden = false;
}
}
[cell.deleteButton addTarget:self action:@selector(deleteCellButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

(5)- (void)collectionView: didSelectItemAtIndexPath:的实现如下:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//取出是某一个section;
SectionModel *sec = [self.dataSectionArray objectAtIndex:indexPath.section];
if ((indexPath.row == sec.cellArray.count - 1)) {
NSLog(@"点击最后一个cell,执行添加操作");
//初始化一个新的cell模型;
CellModel *cel = [[CellModel alloc] init];
cel.cellImage = @"1";
cel.cellDesc = @"再来一个";
//获取当前的cell数组;
self.dataCellArray = sec.cellArray;
//把新创建的cell插入到最后一个之前;
[self.dataCellArray insertObject:cel atIndex:self.dataCellArray.count - 1];
//更新UI;
[self.collectionView reloadData];
}else{
NSLog(@"第%ld个section,点击图片%ld",indexPath.section,indexPath.row);
}
}

(6)懒加载:在UI的动态更新中,懒加载是经常会用到的手段。具体实现如下:
懒加载section数组:
- (NSMutableArray *)dataSectionArray {
if (!_dataSectionArray){
//CollectionView有一个Section数组;
_dataSectionArray = [[NSMutableArray alloc] initWithCapacity:2];//1个;
for (int i = 0; i < 2; i++) {
//默认初始有两个Section;
_dataCellArray = [[NSMutableArray alloc] initWithCapacity:6];//2个;
for (int j = 0; j < 6; j++) {
//默认一个section中有6个cell;
//初始化每一个cell;
CellModel *cellModel = [[CellModel alloc] init];
cellModel.cellImage = self.cellImageArr[j];
cellModel.cellDesc = self.cellDescArr[j];
//添加到cell数组中;
[_dataCellArray addObject:cellModel];
}
//初始化section;
SectionModel *sectionModel = [[SectionModel alloc] init];
sectionModel.sectionName = self.headerArray[i];
//把上面生成的cell数组加入到section数组中;
sectionModel.cellArray = _dataCellArray;
//增加一个section;
[_dataSectionArray addObject:sectionModel];
}
}
return _dataSectionArray;
}
懒加载cell数组中的图片:
- (NSMutableArray *)cellImageArr{
if (!_cellImageArr) {
self.cellImageArr = [[NSMutableArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",nil];
}
return _cellImageArr;
}
懒加载cell数组中的描述文字:
- (NSMutableArray *)cellDescArr{
if (!_cellDescArr) {
self.cellDescArr = [[NSMutableArray alloc] initWithObjects:@"第0个",@"第1个",@"第2个",@"第3个",@"第4个",@"添加",nil];
}
return _cellDescArr;
}

(7)删除cell的代码实现,首先需要点击右上角的编辑按钮,使所有的cell处于可删除状态:
- (IBAction)editButtonPressed:(id)sender {
//从正常状态变为可删除状态;
if (CellState == NormalState) {
CellState = DeleteState;
self.editButton.titleLabel.text = @"完成";
//循环遍历整个CollectionView;
for(CollectionViewCell *cell in self.collectionView.visibleCells) {
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
//找到某一个具体的section;
SectionModel *section = self.dataSectionArray[indexPath.section];
//除最后一个cell外都显示删除按钮;
if (indexPath.row != section.cellArray.count - 1){
[cell.deleteButton setHidden:false];
}
}
}
else if (CellState == DeleteState){
CellState = NormalState;
self.editButton.titleLabel.text = @"编辑";
}
[self.collectionView reloadData];
}
(8)点击每一个删除按钮:
- (void)deleteCellButtonPressed: (id)sender{
//获取cell
CollectionViewCell *cell = (CollectionViewCell *)[sender superview];
//获取cell对应的indexpath;
NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];
//删除cell;
SectionModel *sec = [self.dataSectionArray objectAtIndex:indexpath.section];
[sec.cellArray removeObjectAtIndex:indexpath.row];
[self.collectionView reloadData];
NSLog(@"删除按钮,section:%ld , row: %ld",(long)indexpath.section,(long)indexpath.row);
}
(9)以上为核心代码实现,其他部分的源代码可以从Github上下载。
原文链接:http://blog.csdn.net/chenyufeng1991/article/details/50143417
demo地址获取(原作者):https://github.com/chenyufeng1991/CollectionView

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,094评论 4 62
  • 你准备好了么 跟2015年说再见 这一年最火的十大旅行地 你去过几个? 第十名:香港 香港,是个适合对所有人的旅行...
    财神出位阅读 390评论 0 1
  • 欧洲,地中海,爱琴海,意大利,布拉卡的斯皮纳龙格小岛。岛上居住的是被强制隔离的麻风病人,仅靠一条小船与大陆之间保持...
    曹门霞客行阅读 878评论 3 4
  • 姓名:楼灵芝 单位:杭州熙林服饰 【日精进打卡第148天】 【知~学习】 《六项精进》背诵2遍,共589遍; 《大...
    心镜_8ef4阅读 118评论 0 0
  • (负能量满满的小文章) 两个观点不同的人往往会在一些小事上产生极大的矛盾。 比如说我和我爸。 就在今天,在餐馆里吃...
    TIQ阅读 401评论 0 0