IOS 모방 경 동 주소 선택 기

10627 단어
프로젝트 는 주소 선택 기 가 필요 합 니 다. 동적 네트워크 나 로 컬 데이터 베이스 에서 다음 주 소 를 가 져 와 야 합 니 다. 그리고 우연히 경 동의 주소 선택 기 가 좋 은 것 을 보고 모방 하 였 습 니 다.디자인 모드 는 UItableView 의 데이터 원본 과 프 록 시 모드 를 모방 한 것 입 니 다. 그 중에서 데이터 원본 이 block 으로 되 돌아 갈 수 있 는 데이터 원본 은 네트워크 요청 이기 때문에 block 으로 되 돌 리 는 것 이 편리 합 니 다.코드 는 다음 과 같 습 니 다:
#import 
#import "SFSelectModel.h"
#import "SFSelectTableViewCell.h"
typedef void (^SoureBlock)(NSMutableArray *models);
@protocol SFSelectVCDataSource 
- (void)sFSelectVCSourceBackWithDepth:(NSInteger)depth regionId:(NSInteger)regionId block:(SoureBlock)block;
@end
@protocol SFSelectVCDelegate 
- (void)sFSelectVCDidChange:(NSString*)adress firstRegionId:(NSInteger)firstRegionId lastRegionId:(NSInteger)lastRegionId;
@end
@interface SFSelectVC : UIViewController
@property (nonatomic ,weak) id dataSource;
@property (nonatomic ,weak) id delegate;


- (instancetype)initWithFirstData:(NSMutableArray*)firstData;
@end

. m 파일:
//
//  SFSelectVC.m
//  iseasoftCompany
//
//  Created by songfei on 2018/4/19.
//  Copyright © 2018  hycrazyfish. All rights reserved.
//

#import "SFSelectVC.h"
#import "SFTitleView.h"
static NSString *const CellID = @"CellID";
@interface SFSelectVC ()
@property (nonatomic, strong) SFTitleView *titleView;//     
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray*> *allModelArray;//     
@property (nonatomic ,strong) NSMutableArray *midelModels;//    
@end

@implementation SFSelectVC{
    SFSelectModel *lastSelctcModel;
    NSMutableArray *btnArry;
}

- (instancetype)initWithFirstData:(NSMutableArray*)firstData{
    if (self = [super init]) {
        self.midelModels = firstData;
    }
    return self;
}

- (SFTitleView *)titleView{
    if (!_titleView) {
        _titleView = [[SFTitleView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
        _titleView.backgroundColor = [UIColor whiteColor];
        __weak typeof (self)weakSelf = self;
        _titleView.btnBlock = ^(NSInteger onClickBtn) {
            [weakSelf onClickBtn:onClickBtn];
        };
    }
    return _titleView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.tableFooterView = [UIView new];//         
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[SFSelectTableViewCell class] forCellReuseIdentifier:CellID];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5f];
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.tableView];
    [self setMasnory];
    btnArry = [NSMutableArray array];
    self.allModelArray = [NSMutableArray array];
    [self.tableView reloadData];
}
- (void)setMasnory{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(300);
    }];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.tableView.mas_top);
        make.left.mas_equalTo(self.tableView.mas_left);
        make.right.mas_equalTo(self.tableView.mas_right);
        make.height.mas_equalTo(44);
    }];
  
}

#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.midelModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SFSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    if(!cell){
        cell = [[SFSelectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    [cell setModel:self.midelModels[indexPath.row]];
    return cell;
}

#pragma mark --UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    __weak typeof (self)weakSelf = self;
    SFSelectModel *clickModel = self.midelModels[indexPath.row];
    
    NSIndexPath *lastIndexPath;
    if (lastSelctcModel == clickModel){
        
    }else if (lastSelctcModel.depth == clickModel.depth){
        if (lastSelctcModel) {
            NSInteger row = [self.midelModels indexOfObject:lastSelctcModel];
            lastIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
        }
        
        for (SFSelectModel *mod in self.midelModels) {
            mod.isSelect = false;
        }
        self.midelModels[indexPath.row].isSelect = true;
        
        [self.titleView removeBtnWitBtn:btnArry.lastObject];
        [btnArry removeLastObject];
        UIButton *btn = [self.titleView addOneButton:clickModel.name];
        [btnArry addObject:btn];
        
        lastSelctcModel = clickModel;
        
    }else{
        lastSelctcModel = clickModel;
        self.midelModels[indexPath.row].isSelect = true;
        
        UIButton *btn = [self.titleView addOneButton:clickModel.name];
        [btnArry addObject:btn];
    }
    
    
    if (self.dataSource) {
        [self.dataSource sFSelectVCSourceBackWithDepth:clickModel.depth+1 regionId:clickModel.regionId block:^(NSMutableArray *models) {
            if (models.count > 0) {
                if (!(weakSelf.allModelArray.lastObject.lastObject.depth == weakSelf.midelModels.lastObject.depth) || weakSelf.allModelArray.count == 0) {
                    [weakSelf.allModelArray addObject:weakSelf.midelModels];
                }
                
                 weakSelf.midelModels = models;
                [weakSelf.tableView reloadData];
            }else{
                if (!(weakSelf.allModelArray.lastObject.lastObject.depth == weakSelf.midelModels.lastObject.depth) || weakSelf.allModelArray.count == 0) {
                    [weakSelf.allModelArray addObject:weakSelf.midelModels];
                }
                
                if (lastIndexPath) {
                    [weakSelf.tableView reloadRowsAtIndexPaths:@[lastIndexPath] withRowAnimation:UITableViewRowAnimationNone];
                }
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
        }];
    }

}

#pragma mark --   BTN
- (void)onClickBtn:(NSInteger)index{
    if (index < 0) {
        if (self.delegate) {
            NSInteger firstRegionId = 0;
            NSInteger lastRegionId = 0;
            NSString *adressStr = @"";
            for (int i = 0; i < self.allModelArray.count; i++) {
                if (i == 0) {
                    for (SFSelectModel *model in self.allModelArray[i]) {
                        if (model.isSelect) {
                            firstRegionId = model.regionId;
                            adressStr = [adressStr stringByAppendingString:model.name];
                        }
                    }
                }else if (i == (self.allModelArray.count - 1)){
                    for (SFSelectModel *model in self.allModelArray[i]) {
                        if (model.isSelect) {
                            lastRegionId = model.regionId;
                             adressStr = [adressStr stringByAppendingString:model.name];
                        }
                    }
                }else{
                    for (SFSelectModel *model in self.allModelArray[i]) {
                        if (model.isSelect) {
                            lastRegionId = model.regionId;
                            adressStr = [adressStr stringByAppendingString:model.name];
                        }
                    }
                }
                
            }
            if ([adressStr isEqualToString:@""]) {
                [self dismissViewControllerAnimated:true completion:nil];
                return;
            }
            [self.delegate sFSelectVCDidChange:adressStr firstRegionId:firstRegionId lastRegionId:lastRegionId];
        }
        [self dismissViewControllerAnimated:true completion:nil];
        return;
    }
    
    
    if (btnArry.count == (index +1)) {
        return;
    }
    
    for (NSInteger i = index + 1; i < btnArry.count; i++) {
        UIButton* btn = btnArry[I];
        [self.titleView removeBtnWitBtn:btn];
    }
    
    [btnArry removeObjectsInRange:NSMakeRange(index+1, btnArry.count - (index + 1))];
    [self.allModelArray removeObjectsInRange:NSMakeRange(index+1, self.allModelArray.count - (index +1))];
    self.midelModels = self.allModelArray.lastObject;
    
    [self.tableView reloadData];
    
    NSIndexPath *nowPath;
    for (SFSelectModel *model in self.midelModels) {
        if (model.isSelect) {
            NSInteger atNub = [self.midelModels indexOfObject:model];
            nowPath = [NSIndexPath indexPathForRow:atNub inSection:0];
            lastSelctcModel = model;
        }
    }
    
    [self.tableView scrollToRowAtIndexPath:nowPath atScrollPosition:UITableViewScrollPositionMiddle animated:true];
}


//        
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    point = [self.tableView.layer convertPoint:point fromLayer:self.view.layer];
    if (![self.tableView.layer containsPoint:point]) {
        point = [[touches anyObject] locationInView:self.view];
        point = [self.titleView.layer convertPoint:point fromLayer:self.view.layer];
        if (![self.titleView.layer containsPoint:point]){
           [self dismissViewControllerAnimated:true completion:nil];
        }
    }
}
@end

코드 Git 링크:https://github.com/songfeiSF/-SFAdressSelectVC-
그림:
QQ20180420-163409-HD.gif

좋은 웹페이지 즐겨찾기