苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

分布式系统框架(V1.3) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper万能框架(V2.3-含.netcore) HttpHelper官方出品,无敌框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V1.9) 最牛的爬虫类,没有爬不到只有想不到 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 244|回复: 5

[综合] 【iOS开发实用框架】仿淘宝地址三级选择

[复制链接]
发表于 2019-1-7 17:18:05 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 2019-1-7 17:26 编辑

   导读

【iOS开发实用框架】基础教程目录    http://www.sufeinet.com/thread-24348-1-1.html
iOS开发实用框架】实例目录贴    http://www.sufeinet.com/thread-24654-1-1.html


效果:
5.gif

代码:

SelectAddressVC.h

[Objective-C] 纯文本查看 复制代码
#import "BaseViewController.h"
#import "ItemModel.h"

////地址选择:三级 五级
//typedef NS_ENUM(NSInteger, AddressLevel)
//{
// AddressLevel_Three,
// AddressLevel_Four,
// AddressLevel_Five,
//};

//地址选择类型:省、市、区、县、镇/乡、村
typedef NS_ENUM(NSInteger, AddressType)
{
AddressType_Province, //省
AddressType_City, //市
AddressType_Area, //区
};

typedef void(^DidSelectBlock)(ItemModel *setModel,NSInteger addressType);

@interface SelectAddressVC : BaseViewController

@property(nonatomic,strong) NSArray *aryList;
@property(nonatomic,strong) NSString *selectName;

@property (nonatomic,assign) NSInteger addressLevel;

@property(nonatomic,copy) DidSelectBlock didSelectBlock;

@end


SelectAddressVC.m

[Objective-C] 纯文本查看 复制代码
#import "SelectAddressVC.h"
#import "HMSegmentedControl.h"
#import "TitleSelectCell.h"
#import "ImageClickView.h"
#import "TitleView.h"

#define ContentViewHeight Height(385)
#define ThreeSegmentWidth vContent.frameWidth/4
#define FiveSegmentWidth vContent.frameWidth/5

@interface SelectAddressVC ()<UITableViewDelegate,UITableViewDataSource>{
UIView *vContent;

HMSegmentedControl *segAddress;

UITableView *tabSelect;


NSString *strProvince;
NSInteger strProvinceId;

NSString *strCity;
NSInteger strCityId;

NSString *strArea;
NSInteger strAreaId;

NSString *strCountry;
NSInteger strCountryId;

NSString *strTown;
NSInteger strTownId;
}

@end

@implementation SelectAddressVC

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];

[self initSubViews];

_aryList = [ItemModel mj_objectArrayWithKeyValuesArray:@[@{@"ID":@3,@"Name":@"河南"},@{@"ID":@12,@"Name":@"河北"},@{@"ID":@10,@"Name":@"江苏"}]];

[tabSelect reloadData];
}

-(void)initSubViews{

vContent = [UIView new];
vContent.frame = CGRectMake(0, ScreenHeight - ContentViewHeight, ScreenWidth, ContentViewHeight);
vContent.backgroundColor = kColor(whiteColor);
[self.view addSubview:vContent];

UILabel *lblTitle = [UILabel new];
lblTitle.frame = CGRectMake(0, 0, vContent.frameWidth, 45);
lblTitle.text = @"所在地";
lblTitle.textColor = Color_333;
lblTitle.font = kFont(14);
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.frameWidth = getLabelTextWidth(lblTitle);
lblTitle.frameX = (vContent.frameWidth - lblTitle.frameWidth)/2;
[vContent addSubview:lblTitle];

CGFloat imageSize = 16;
CGFloat margin = 10;
ImageClickView *close = [[ImageClickView alloc]initWithFrame:CGRectMake(vContent.frameWidth - imageSize - 10*2, 0, imageSize + margin*2 , 45) ImageSize:CGSizeMake(imageSize, imageSize)];
close.imageView.image = kImage(@"delete");
[close addTapGestureWithBlock:^(id sender) {
[self dismiss];
}];
[vContent addSubview:close];

UIView *line = drawHLine(Color_eee, CGRectMake(0, lblTitle.frameBottom, vContent.frameWidth, 1));
[vContent addSubview:line];

// CGFloat width = tWidthByFont(13, @"请选择") + 5;
segAddress = [[HMSegmentedControl alloc]initWithSectionTitles:@[@"请选择"]];
segAddress.frame = CGRectMake(10, line.frameBottom,ThreeSegmentWidth, 45);
segAddress.backgroundColor = kColor(clearColor);
segAddress.selectionIndicatorHeight = 1;
segAddress.selectionIndicatorColor = Color_Blue;
segAddress.titleTextAttributes = @{NSForegroundColorAttributeName:Color_333,NSFontAttributeName:kFont(14)};
segAddress.selectedTitleTextAttributes = @{NSForegroundColorAttributeName:Color_Blue,NSFontAttributeName:kFont(14)};
segAddress.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segAddress.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
[segAddress addTarget:self action:@selector(segmentedChange:) forControlEvents:UIControlEventValueChanged];
[vContent addSubview:segAddress];

tabSelect = [UITableView new];
tabSelect.frame = CGRectMake(0, segAddress.frameBottom - 1, vContent.frameWidth, vContent.frameHeight - segAddress.frameBottom);
tabSelect.separatorStyle = UITableViewCellSeparatorStyleNone;
tabSelect.delegate = self;
tabSelect.dataSource = self;
[vContent addSubview:tabSelect];
[vContent bringSubviewToFront:segAddress];
}

#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _aryList.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

// TitleView *vHeader = [[TitleView alloc]initWithFrameHeight:1 LeftMargin:10];
// vHeader.titleLabel.backgroundColor = Color_eee;
UIView *vHeader = [UIView new];
vHeader.backgroundColor = Color_eee;
return vHeader;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self getTitleSelectCellForTableView:tableView AtIndexPath:indexPath];
}

-(TitleSelectCell *)getTitleSelectCellForTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"TitleSelectCell";
TitleSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[TitleSelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.imgSelect.hidden = YES;
}
ItemModel *sModel = _aryList[indexPath.row];
cell.lblTitle.text = sModel.Name;
if ([sModel.Name isEqualToString:strProvince] || [sModel.Name isEqualToString:strCity] || [sModel.Name isEqualToString:strArea] || [sModel.Name isEqualToString:strCountry] || [sModel.Name isEqualToString:strTown]) {
cell.lblTitle.textColor = Color_Blue;
}else{
cell.lblTitle.textColor = Color_333;
}

return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ItemModel *sModel = _aryList[indexPath.row];
_didSelectBlock(sModel,segAddress.selectedSegmentIndex);
switch (segAddress.selectedSegmentIndex) {
case 0:{
strProvince = sModel.Name;
strProvinceId = sModel.ID;
[self updateListArrayByType:AddressType_Province TopId:sModel.ID IsSelectRow:YES];

}break;
case 1:{
strCity = sModel.Name;
strProvinceId = sModel.ID;
[self updateListArrayByType:AddressType_City TopId:sModel.ID IsSelectRow:YES];
}break;
case 2:{
strArea = sModel.Name;
strAreaId = sModel.ID;
[self dismiss];
}break;
default:
break;
}
}

-(void)updateListArrayByType:(NSInteger)type TopId:(NSInteger)topId IsSelectRow:(BOOL)isSelectRow{
switch (type) {
case AddressType_Province:{
_aryList = [ItemModel mj_objectArrayWithKeyValuesArray:@[@{@"ID":@5,@"Name":@"郑州"},@{@"ID":@6,@"Name":@"洛阳"},@{@"ID":@11,@"Name":@"新乡"}]];
[self updateSegmentTitlesByType:type + 1 IsSelectRow:isSelectRow];

}break;
case AddressType_City:{
_aryList = [ItemModel mj_objectArrayWithKeyValuesArray:@[@{@"ID":@501,@"Name":@"金水区"},@{@"ID":@502,@"Name":@"中原区"},@{@"ID":@503,@"Name":@"二七区"}]];
[self updateSegmentTitlesByType:type + 1 IsSelectRow:isSelectRow];
}break;
default:
break;
}
}

-(void)updateSegmentTitlesByType:(NSInteger)type IsSelectRow:(BOOL)isSelectRow{
if (isSelectRow) {
if (type == 1) {
segAddress.sectionTitles = @[strProvince,@"请选择"];
}if (type == 2) {
segAddress.sectionTitles = @[strProvince,strCity,@"请选择"];
}if (type == 3) {
segAddress.sectionTitles = @[strProvince,strCity,strArea,@"请选择"];
}if (type == 4) {
segAddress.sectionTitles = @[strProvince,strCity,strArea,strCountry,@"请选择"];
}
segAddress.frameWidth = ThreeSegmentWidth * (type + 1);
segAddress.selectedSegmentIndex = type;
}
[tabSelect reloadData];
}

-(void)segmentedChange:(HMSegmentedControl *)sender{

switch (sender.selectedSegmentIndex) {
case 0:{
_aryList = [ItemModel mj_objectArrayWithKeyValuesArray:@[@{@"ID":@3,@"Name":@"河南"},@{@"ID":@12,@"Name":@"河北"},@{@"ID":@10,@"Name":@"江苏"}]];
[tabSelect reloadData];
}break;
case 1:{
[self updateListArrayByType:AddressType_City TopId:strCityId IsSelectRow:NO];
}break;

default:
break;
}
}

-(void)dismiss{
[self.view removeFromSuperview];
[self removeFromParentViewController];
}


注:例子中的数据只模拟了河南郑州的,使用时,数据直接从服务器请求填充即可。

附件: 仿淘宝地址管理选择.zip (766.96 KB, 下载次数: 1)
发表于 2019-1-7 17:18:49 | 显示全部楼层
发表于 2019-1-7 17:26:46 | 显示全部楼层
发表于 2019-1-7 17:28:53 | 显示全部楼层
发表于 2019-1-7 17:46:12 | 显示全部楼层
发表于 2019-1-7 20:49:09 | 显示全部楼层
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备17001017号-1)

GMT+8, 2019-6-19 02:45

© 2017-2018

快速回复 返回顶部 返回列表