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

来源:互联网 发布:淘宝买家最多买多少 编辑:程序博客网 时间:2024/06/05 06:02

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



//

//  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 [[selfalloc] init];

}


- (instancetype)initWithFrame:(CGRect)frame

{

   if (self = [superinitWithFrame:frame]) {

        [selfsetUpSubViews];

    }

    

    returnself;

}


- (instancetype)initWithCoder:(NSCoder *)decoder

{

   if (self = [superinitWithCoder:decoder]) {

        [selfsetUpSubViews];

    }

    returnself;

}


- (void)setUpSubViews

{

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

    [selfaddSubview:bgView];

   self.bgView = bgView;

    

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

    [bgViewaddSubview:addView];

    

   UIButton *printBtn = [[UIButtonalloc] init];

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

    printBtn.backgroundColor = [[UIColorgreenColor] colorWithAlphaComponent:0.5f];

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

    [printBtn setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

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

    [selfaddSubview:printBtn];

   self.printBtn = printBtn;

    

    // 默认设置有一个

   self.viewCount =0;

}


- (UIView *)addANewViewWithFrame:(CGRect)frame

{

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

    // 添加按钮

    UIButton *addBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

    [addBtn setImage:[UIImageimageNamed:@"tianjia"]forState:UIControlStateNormal];

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

    [newViewaddSubview:addBtn];

    

    // 文本框

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

    textField.placeholder =@"请编辑";

    textField.backgroundColor = [UIColorwhiteColor];

    textField.font = [UIFontsystemFontOfSize:14];

    [newViewaddSubview:textField];

    

   return newView;

}


#pragma mark - method

- (void)addButtonClick:(UIButton *)addBtn

{

    // 0.退出第一响应者

    [selfendEditing:YES];

    

   if ([addBtn.currentImageisEqual:[UIImageimageNamed:@"shanchu"]]) {// 删除

       UIView *superView = addBtn.superview;

        [superViewremoveFromSuperview];

        // 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 = [[UIAlertViewalloc] initWithTitle:nilmessage:@"至多只能添加5"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];

            [alertViewshow];

           return;

        }

        

        // view个数增加一个

       self.viewCount++;

        [addBtn setImage:[UIImageimageNamed:@"shanchu"]forState:UIControlStateNormal];

        // 创建一个新的view

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

        [self.bgViewaddSubview:secondView];

    }

    

    // 修改视图的frame

    self.frame =CGRectMake(50,100, [UIScreenmainScreen].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.退出第一响应者

    [selfendEditing:YES];

    

    // 1.取出所有的文字

    NSMutableArray *contents = [NSMutableArrayarray];

    

   for (UIView *subViewin self.bgView.subviews) {

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

        [contentsaddObject:tempTextField.text];

    }

    

    // 2.通知代理

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

        [self.delegatecustomAddView:selfdidClickPrintBtnWithContentsArr: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 {

    [superviewDidLoad];

    

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

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

    buttonAddView.backgroundColor = [[UIColorredColor] colorWithAlphaComponent:0.3];

    buttonAddView.delegate =self;

    [self.viewaddSubview:buttonAddView];

    

    

    // 1.数据展示

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

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

    showLabel.font = [UIFontsystemFontOfSize:14];

    showLabel.textColor = [UIColorblackColor];

    showLabel.numberOfLines =0;

    showLabel.textAlignment =NSTextAlignmentCenter;

    [self.viewaddSubview:showLabel];

   self.showLabel = showLabel;

}


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

    [supertouchesBegan:touches withEvent:event];

    [self.viewendEditing: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 = [NSStringstringWithFormat:@"%ldtext:%@", i + 1, contentsArr[i]];

        }else {

            labelTextStr = [NSStringstringWithFormat:@"%@\n%ldtext:%@", labelTextStr, i + 1, contentsArr[i]];

        }

    }

   self.showLabel.text = labelTextStr;

}

@end






阅读全文
1 0
原创粉丝点击