历史数据删除控件

2020-07-15  本文已影响0人  哥只是个菜鸟
#import <UIKit/UIKit.h>

@class DropListCell;
NS_ASSUME_NONNULL_BEGIN
@protocol DeleteAccountDelegate <NSObject>

- (void)deleteAccount:(DropListCell *)selectcell;

@end
@interface DropListCell : UITableViewCell
@property(nonatomic,weak)id<DeleteAccountDelegate>  delegate;
@property (nonatomic,strong)NSDictionary *models;
@end

NS_ASSUME_NONNULL_END
#import "DropListCell.h"
@interface DropListCell()
@property (strong, nonatomic) UIImageView *headImageView;
@property (strong, nonatomic) UILabel *accountLabel;
@property (strong, nonatomic) UIButton *deleteButton;
@end

@implementation DropListCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor=[UIColor colorFromHexString:@"#F5F6F7"];
    self.headImageView=[[UIImageView alloc]initWithFrame:CGRectZero];
    self.headImageView.image=[UIImage imageNamed:@"ABPSettingsResource.bundle/ic_about_logo.png"];
    [self addSubview:self.headImageView];
    
    self.deleteButton=[[UIButton alloc]initWithFrame:CGRectZero];
    [self.deleteButton setImage:[UIImage imageNamed:@"ABPSettingsResource.bundle/logindel.png"] forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteAccount:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.deleteButton];
    
    self.accountLabel=[[UILabel alloc]initWithFrame:CGRectZero];
      self.accountLabel.textAlignment=NSTextAlignmentCenter;
      [self addSubview:self.accountLabel];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15.f);
        make.centerY.equalTo(self);
        make.width.equalTo(30.f);
        make.height.equalTo(30.f);
    }];
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.headImageView.mas_right).offset(15.f);
        make.centerY.centerX.equalTo(self);
    }];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-10.f);
        make.centerY.equalTo(self);
        make.width.right.equalTo(30.f);
    }];
}

- (void)setModels:(NSDictionary *)models{
    if(models[@"headImage"]){
         self.headImageView.image=[UIImage imageWithData:models[@"headImage"]];
    }else{
        self.headImageView.image= [UIImage imageNamed:@""];
    }
    self.accountLabel.text=isnull(models[@"aliasValue"]);
}

- (void)deleteAccount:(UIButton *)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(deleteAccount:)]){
        [self.delegate deleteAccount:self];
    }
}
@end
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface DropDownListView : UIView
- (instancetype)initWithTitleArray:(NSArray *)titleA orignPoint:(CGPoint)orginPoint listWidth:(CGFloat)listWidth selectResult:(void (^)(NSDictionary * accountDict))selectResult deleteDict:(void (^)(NSDictionary * deleteDict, NSDictionary *nextDict))deleteDict;

-(void)show;

-(void)dismiss;
@end

NS_ASSUME_NONNULL_END
#import "DropDownListView.h"
#import "ABPAccountListCell.h"
@interface DropDownListView ()<UITableViewDelegate,UITableViewDataSource,DeleteAccountDelegate,UIAlertViewDelegate>
@property(nonatomic,assign)CGPoint orignPoint;
@property(nonatomic,assign)CGFloat listWidth;
@property(nonatomic,copy)void (^selectResult)(NSDictionary * accountDict);
@property(nonatomic,copy)void (^deleteDict)(NSDictionary * deleteDict ,NSDictionary *nextDict);
@property(nonatomic,strong)NSMutableArray *titleA;
@property(nonatomic,strong)UITableView *listView;
@property(nonatomic,assign)NSInteger currentIndex;
@end
@implementation DropDownListView
-(instancetype)initWithTitleArray:(NSArray *)titleA orignPoint:(CGPoint)orginPoint listWidth:(CGFloat)listWidth selectResult:(void (^)(NSDictionary *))selectResult deleteDict:(nonnull void (^)(NSDictionary * _Nonnull, NSDictionary * _Nonnull))deleteDict{
    self = [super init];
    if (self) {
        self.orignPoint = orginPoint;
        //超出屏幕宽度的判断
        if (listWidth +  self.orignPoint.x > [UIScreen mainScreen].bounds.size.width) {
            listWidth = [UIScreen mainScreen].bounds.size.width;
        }
        self.titleA=[NSMutableArray array];
        self.listWidth = listWidth;
        self.selectResult = selectResult;
        self.deleteDict = deleteDict;
        self.titleA = [titleA mutableCopy];
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor =[UIColor clearColor];
        [self setUp];
    }
    
    return self;
}
-(void)setUp{
    
    [self addSubview:self.listView];
  
}


-(void)show{
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    [self creatAnimaiton];
      
}

-(void)creatAnimaiton{

    [self.listView setFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, 0)];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
        [self.listView setFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, self.titleA.count * 50)];
        
          [self.listView reloadData];
    } completion:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.listView reloadData];
    });
  
    
}
-(void)dismiss{
//    [self.listView setFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, self.titleA.count * 50)];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.alpha = 0.0;
//                self.listView.transform = CGAffineTransformMakeTranslation(0, -self.titleA.count * 50);
                         [self.listView setFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, 0)];
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         [self.listView removeFromSuperview];
                         
                     }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleA.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ABPAccountListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[ABPAccountListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
     [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    NSDictionary *models=self.titleA[indexPath.row];
    cell.models=models;
    cell.delegate=self;
    return cell;
}
-(UITableView *)listView{
    if (!_listView) {
        _listView =[[UITableView alloc]initWithFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, self.titleA.count * 50) style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        _listView.rowHeight = 50;
        [_listView registerClass:[ABPAccountListCell class] forCellReuseIdentifier:@"cell"];
        _listView.layer.cornerRadius =5;
        _listView.backgroundColor=[UIColor colorFromHexString:@"#F5F5F5"];
        _listView.layer.masksToBounds = YES;
    }
    return _listView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *selectstr=self.titleA[indexPath.row];
    if (self.selectResult) {
        self.selectResult(selectstr);
        [self dismiss];
    }
}

#pragma mak--
- (void)deleteAccount:(ABPAccountListCell *)selectcell{
      NSInteger indexs=[self.listView indexPathForCell:selectcell].row;
    self.currentIndex=indexs;
     NSDictionary *dicts=self.titleA[indexs];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                                                  message:[NSString stringWithFormat:@"確認刪除賬號 %@ 嗎?",dicts[@"aliasValue"]]
                                                                                 delegate:self
                                                                        cancelButtonTitle:@"取消"
                                                                        otherButtonTitles:@"删除", nil];
                              
                              alertView.delegate = self;
                              [alertView show];
  
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        
    }else  if(buttonIndex==1){
          NSDictionary *currentDict=self.titleA[self.currentIndex];
        [self.titleA removeObjectAtIndex:self.currentIndex];
        [[NSUserDefaults standardUserDefaults]setObject:self.titleA forKey:HistoryAccount];
        [self.listView reloadData];
        [self setNeedsLayout];
        [UIView animateWithDuration:0.3f
                                animations:^{
                                    [self.listView setFrame:CGRectMake(self.orignPoint.x, self.orignPoint.y, self.listWidth, self.titleA.count * 50)];
                                }
                                completion:^(BOOL finished){
                                    
           }];
       
        if(self.titleA.count==0 || self.titleA==nil){
          
        }else{
            NSInteger nextIndex=self.currentIndex;
            if(nextIndex>self.titleA.count-1){
                nextIndex=0;
            }
          
            NSDictionary *nextDict=self.titleA[nextIndex];
                    if(self.deleteDict){
                               self.deleteDict(currentDict,nextDict);
                    }
        }
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch =[touches anyObject];

    CGPoint point = [touch locationInView:self];
    point = [self.listView.layer convertPoint:point fromLayer:self.layer];
    if ([self.listView.layer containsPoint:point]  ) {

    }else{
        [self dismiss];
        if (self.selectResult) {
            self.selectResult(nil);
          }

    }

}

@end
上一篇下一篇

猜你喜欢

热点阅读