IOS 모방 경 동 주소 선택 기
#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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.