|
本帖最后由 竹林风 于 2019-1-7 17:26 编辑
导读
效果:
代码:
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)
|
|