iOS开发-模仿动态增加或者删除cell并自动增加变化高度

今天给同学们讲一下在项目开发中我们经常会碰到这样的需求,动态的添加或者删除某一行显示数据并且重新布局Frame,那么废话不多说,直接上代码!先看演示视频:

iOS开发-模仿动态增加或者删除cell并自动增加变化高度.gif
//

//  ZZCustomAddView.h

//  动态变化frame

//

//  Created by new on 2017/7/21.

//  Copyright © 2017年 we-smart Co., LTD. All rights reserved.

//

#import <UIKit/UIKit.h>

@class  ZZCustomAddView;

@protocol ZZCustomAddViewDelegate <NSObject>

@optional

- (void)customAddView:(ZZCustomAddView *)customAddView didClickPrintBtnWithContentsArr:(NSMutableArray *)contentsArr;

@end

@interface ZZCustomAddView : UIView

+ (instancetype)customAddView;

@property (weak, nonatomic) id <ZZCustomAddViewDelegate> delegate;

@end

//

//  ZZCustomAddView.m

//  动态变化frame

//

//  Created by new on 2017/7/21.

//  Copyright © 2017年 we-smart Co., LTD. All rights reserved.

//

// 添加按钮的父类view的高度

#define kViewHeight 40

// 添加按钮的宽

#define kAddBtnWidth 25

#import "ZZCustomAddView.h"

@interface  ZZCustomAddView()

/**

 *  view的个数

 */

@property (assign, nonatomic) NSInteger viewCount;

/**

 *  添加的新的view

 */

@property (weak, nonatomic) UIView *bgView;

/**

 *  打印按钮

 */

@property (weak, nonatomic) UIButton *printBtn;

@end

@implementation ZZCustomAddView

+ (instancetype)customAddView

{

    return [[self alloc] init];

}

- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        [self  setUpSubViews];

    }

 return  self;

}

- (instancetype)initWithCoder:(NSCoder *)decoder

{

    if (self = [super initWithCoder:decoder]) {

        [self  setUpSubViews];

    }

 return  self;

}

- (void)setUpSubViews

{

    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kViewHeight)];

    [self addSubview:bgView];

    self.bgView = bgView;

    UIView *addView = [self addANewViewWithFrame:CGRectMake(0, 0, self.frame.size.width, kViewHeight)];

    [bgView addSubview:addView];

    UIButton *printBtn = [[UIButton alloc] init];

    printBtn.frame = CGRectMake((self.frame.size.width - 80) / 2, CGRectGetMaxY(bgView.frame) + 5, 80, 30);

    printBtn.backgroundColor = [[UIColor  greenColor] colorWithAlphaComponent:0.5f];

    [printBtn setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateNormal];

    [printBtn setTitleColor:[UIColor  whiteColor] forState:UIControlStateNormal];

    [printBtn addTarget:self  action:@selector(printBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:printBtn];

    self.printBtn = printBtn;

 // 默认设置有一个

    self.viewCount = 0;

}

- (UIView *)addANewViewWithFrame:(CGRect)frame

{

    UIView *newView = [[UIView alloc] initWithFrame:frame];

 // 添加按钮

 UIButton *addBtn = [UIButton  buttonWithType:UIButtonTypeCustom];

 addBtn.frame = CGRectMake(10, (kViewHeight - kAddBtnWidth) / 2, kAddBtnWidth, kAddBtnWidth);

[addBtn setImage:[UIImage  imageNamed:@"tianjia"] forState:UIControlStateNormal];

[addBtn addTarget:self  action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    [newView addSubview:addBtn];

 // 文本框

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addBtn.frame) + 5, 5, newView.frame.size.width - CGRectGetMaxX(addBtn.frame) - 5 - 10, kViewHeight - 10)];

    textField.placeholder = @"请编辑";

    textField.backgroundColor = [UIColor  whiteColor];

    textField.font = [UIFont systemFontOfSize:14];

    [newView addSubview:textField];

    return newView;

}

#pragma mark - method

- (void)addButtonClick:(UIButton *)addBtn

{

 // 0.退出第一响应者

    [self  endEditing:YES];

    if ([addBtn.currentImage isEqual:[UIImage imageNamed:@"shanchu"]]) { // 删除

        UIView *superView = addBtn.superview;

        [superView removeFromSuperview];

 // 1.删除到最后一个视图

        if (self.bgView.subviews.count == 1) {

            UIView *currentView = self.bgView.subviews[0];

            currentView.frame = CGRectMake(0, 0, self.bgView.frame.size.width, kViewHeight);

        } else { // 有多个视图

            UIView *firstView = self.bgView.subviews[0];

            CGFloat firstViewY = firstView.frame.origin.y;

            if (firstViewY == 40) { // 第一个已经删除

                firstView.frame = CGRectMake(0, 0, self.bgView.frame.size.width, kViewHeight);

            }

            for (NSInteger i = 0; i < self.bgView.subviews.count; i++) {

                if (i > 0) {

                    UIView *currentView = self.bgView.subviews[I];

                    UIView *frontView = self.bgView.subviews[i - 1];

                    if (CGRectGetMaxY(currentView.frame) - CGRectGetMaxY(frontView.frame) != kViewHeight) { // 判断两个View之间y的距离

                        CGRect frame = currentView.frame;

                        frame.origin = CGPointMake(0, currentView.frame.origin.y - 40);

                        currentView.frame = frame;

                    }

                }

            }

        }

 // view个数减一个

        self.viewCount--;

    } else { // 添加

        if (self.viewCount == 4) { // 限制只能添加5个

 // 这是个过期的方法,可以自行换掉

 UIAlertView *alertView = [[UIAlertView  alloc] initWithTitle:nil  message:@"至多只能添加5个"  delegate:nil  cancelButtonTitle:@"确定"  otherButtonTitles:nil];

            [alertView show];

            return;

        }

 // view个数增加一个

        self.viewCount++;

[addBtn setImage:[UIImage  imageNamed:@"shanchu"] forState:UIControlStateNormal];

 // 创建一个新的view

        UIView *secondView = [self addANewViewWithFrame:CGRectMake(0, self.viewCount * kViewHeight, self.bgView.frame.size.width, kViewHeight)];

        [self.bgView addSubview:secondView];

    }

 // 修改视图的frame

 self.frame = CGRectMake(50, 100, [UIScreen  mainScreen].bounds.size.width - 100, kViewHeight * self.bgView.subviews.count + 40);

 self.bgView.frame = CGRectMake(0, 0, self.frame.size.width, kViewHeight * self.bgView.subviews.count);

    self.printBtn.frame = CGRectMake((self.frame.size.width - 80) / 2, CGRectGetMaxY(self.bgView.frame) + 5, 80, 30);

}

- (void)printBtnClick

{

 // 0.退出第一响应者

    [self  endEditing:YES];

 // 1.取出所有的文字

 NSMutableArray *contents = [NSMutableArray  array];

    for (UIView *subView in self.bgView.subviews) {

        UITextField *tempTextField = (UITextField *)subView.subviews[1];

        [contents addObject:tempTextField.text];

    }

 // 2.通知代理

    if ([self.delegate respondsToSelector:@selector(customAddView:didClickPrintBtnWithContentsArr:)]) {

        [self.delegate  customAddView:self  didClickPrintBtnWithContentsArr:contents];

    }

}

@end

//

//  ViewController.h

//  动态变化frame

//

//  Created by new on 2017/7/21.

//  Copyright © 2017年 we-smart Co., LTD. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//

//  ViewController.m

//  动态变化frame

//

//  Created by new on 2017/7/21.

//  Copyright © 2017年 we-smart Co., LTD. All rights reserved.

//

#import "ViewController.h"

#import "ZZCustomAddView.h"

@interface  ViewController ()<ZZCustomAddViewDelegate>

@property (weak, nonatomic) UILabel *showLabel;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super  viewDidLoad];

 // 0.初始化添加的自定义view

    ZZCustomAddView *buttonAddView = [[ZZCustomAddView alloc] initWithFrame:CGRectMake(50, 100, [UIScreen mainScreen].bounds.size.width - 100, 80)];

    buttonAddView.backgroundColor = [[UIColor  redColor] colorWithAlphaComponent:0.3];

    buttonAddView.delegate = self;

    [self.view addSubview:buttonAddView];

 // 1.数据展示

 UILabel *showLabel = [[UILabel  alloc] initWithFrame:CGRectMake(50, [UIScreen  mainScreen].bounds.size.height - 300, [UIScreen  mainScreen].bounds.size.width - 100, 300)];

    showLabel.text = @"数据展示...";

    showLabel.font = [UIFont systemFontOfSize:14];

    showLabel.textColor = [UIColor blackColor];

    showLabel.numberOfLines = 0;

    showLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:showLabel];

    self.showLabel = showLabel;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];

    [self.view endEditing:YES];

}

#pragma mark - ZZCustomAddViewDelegate

- (void)customAddView:(ZZCustomAddView *)customAddView didClickPrintBtnWithContentsArr:(NSMutableArray *)contentsArr

{

    NSString *labelTextStr = @"";

 // 循环取出text

    for (NSInteger i = 0; i < contentsArr.count; i++) {

        if (i == 0) {

            labelTextStr = [NSString stringWithFormat:@"第%ld行text:%@", i + 1, contentsArr[I]];

        } else {

            labelTextStr = [NSString stringWithFormat:@"%@\n第%ld行text:%@", labelTextStr, i + 1, contentsArr[I]];

        }

    }

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,112评论 25 707
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,723评论 2 59
  • 一样的雨声 一样的雨滴 一样的我站在雨里 不一样的是雨的印记 或许生命中有无数场雨 只是这一场我在这里 而你在那里...
    简_图阅读 195评论 1 2
  • 我梦见闪耀的阳光照在一个男人身上,那个熟悉得已是厌倦的声音,在耳边萦绕,那个温馨的场景,又出现在梦里。这个梦出现过...
    㑿离阅读 112评论 1 0
  • 还记得现在火爆的VR吗? 白天 我收到在网上订购的VR。满怀期待的用VR看了一部未来警察的电影。不得不...
    为了生活奋斗着阅读 273评论 1 0