京东选取地址小控件

这东西实现起来并不是很难,就是相对来说啰嗦一点,好了废话不多说 直接上效果

4258779-426154faea9d6f0b.png
4258779-6d2b4a68d20267a3.png

具体效果可以参考京东选取地址的效果哦,由于我不经常发表简书,就直接上图片了,没搞gif。

代码思路:

1.视图结构:标题scrollview+内容scrollview(n个tableview展示数据)

2.主要逻辑:上下scrollview的联动,点击新数据时生产tableview(有时会重用)+titleLabel,内容偏移的处理

哦了,下面直接上代码了,不瞎bb,如果有任何bug和优化的可以@我哦,多谢了,喜欢的话可以点点赞哦

//
//  SelectAddressView.h
//
//  Created by yaoguanglin on 2017/4/14.
//

#import <UIKit/UIKit.h>

@interface SelectAddressView : UIView

-(instancetype)init:(NSDictionary *)inPutInfoDic outPutInfoBlock:(void(^)(NSDictionary *infoDic))outPutInfoBlock;

@end
//
//  SelectAddressView.m
//
//  Created by yaoguanglin on 2017/4/14.
//

#import "SelectAddressView.h"
#import "DigMachineFinanceMacro.h"
#import "CAttention.h"
#import "Masonry.h"

@interface SelectAddressView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)void(^outPutInfoBlock)(NSDictionary *infoDic);
@property(nonatomic,strong)UIScrollView *selectAddressScrollView;
@property(nonatomic,strong)UIScrollView *titlesScrollView;
@property(nonatomic,strong)UIView *moveLineView;
@property(nonatomic,strong)NSMutableDictionary *headTitleMDic;
@property(nonatomic,assign)NSInteger startTag;
@property(nonatomic,strong)NSMutableDictionary *selectIndexPathMDic;
@property(nonatomic,strong)NSMutableDictionary *addressDataMDic;
@end

@implementation SelectAddressView
-(NSMutableDictionary *)headTitleMDic {
    if(!_headTitleMDic) {
        _headTitleMDic=[NSMutableDictionary dictionary];
    }
    return _headTitleMDic;
}

-(NSMutableDictionary *)addressDataMDic {
    if(!_addressDataMDic) {
        _addressDataMDic=[NSMutableDictionary dictionary];
    }
    return _addressDataMDic;
}

-(NSMutableDictionary *)selectIndexPathMDic {
    if(!_selectIndexPathMDic) {
        _selectIndexPathMDic=[NSMutableDictionary dictionary];
    }
    return _selectIndexPathMDic;
}

-(UIView *)moveLineView {
    if(!_moveLineView) {
        _moveLineView=[[UIView alloc]initWithFrame:CGRectZero];
        _moveLineView.backgroundColor=[UIColor redColor];
    }
    return _moveLineView;
}

-(UIScrollView *)titlesScrollView {
    if(!_titlesScrollView) {
        _titlesScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 60-30)];
        _titlesScrollView.showsHorizontalScrollIndicator=NO;
        _titlesScrollView.showsVerticalScrollIndicator=NO;
        _titlesScrollView.backgroundColor=[UIColor clearColor];
        _titlesScrollView.contentSize=CGSizeMake(0, 60-30);
    }
    return _titlesScrollView;
}

-(UIScrollView *)selectAddressScrollView {
    if(!_selectAddressScrollView) {
        _selectAddressScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, self.frame.size.height-60)];
        _selectAddressScrollView.pagingEnabled=YES;
        _selectAddressScrollView.showsHorizontalScrollIndicator=NO;
        _selectAddressScrollView.showsVerticalScrollIndicator=NO;
        _selectAddressScrollView.delegate=self;
        _selectAddressScrollView.tag=500;
    }
    return _selectAddressScrollView;
}

-(UILabel *)titleLabel:(NSInteger)tag text:(NSString *)text{
    UILabel *label=[[UILabel alloc]init];
    label.textColor=[UIColor redColor];
    label.tag=tag;
    label.preferredMaxLayoutWidth=SCREEN_WIDTH/3;
    label.text=text;
    label.font=[UIFont systemFontOfSize:13];
    label.textAlignment=NSTextAlignmentCenter;
    label.userInteractionEnabled=YES;
    [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleLabelAction:)]];
    return label;
}

-(UITableView *)selectAddressTableView:(CGRect)frame tag:(NSInteger)tag {
    UITableView *selectAddressTableView=[[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
    selectAddressTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    selectAddressTableView.tag=tag;
    selectAddressTableView.delegate=self;
    selectAddressTableView.dataSource=self;
    [selectAddressTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"selectAddressTableViewCellReuseID%zd",tag]];
    return selectAddressTableView;
}

-(instancetype)init:(NSDictionary *)inPutInfoDic outPutInfoBlock:(void(^)(NSDictionary *infoDic))outPutInfoBlock{
    if(self=[super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-300)]) {
        self.backgroundColor=[UIColor whiteColor];
        self.outPutInfoBlock=outPutInfoBlock;
        self.startTag=300;
        [self addHeaderView];
        [self addSubview:self.selectAddressScrollView];
        [self firstAddTableViews];
    }
    return self;
}

- (void)addHeaderView {
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,60)];
    [self addSubview:headerView];
    headerView.backgroundColor=[UIColor whiteColor];
    UILabel *titleLabel=[[UILabel alloc]init];
    titleLabel.textColor=HEXVALEUCOLOR(0x666666, 1.0);
    titleLabel.text=@"所在地区";
    titleLabel.font=[UIFont systemFontOfSize:15];
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(5);
        make.centerX.mas_equalTo(headerView);
    }];
    [headerView addSubview:self.titlesScrollView];
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=HEXVALEUCOLOR(0xcccccc, 1.0);
    [headerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.bottom.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_equalTo(0.5);
    }];
}

//首次创建时增加tableview
- (void)firstAddTableViews {
    [self requestData:nil tableViewTag:self.startTag];
}
//刷新tableview和标题栏
- (void)refreshSubviews:(NSInteger)tag infoAry:(NSArray*)infoAry{
    for(NSInteger i=(tag-self.startTag);i<(self.selectAddressScrollView.contentSize.width/SCREEN_WIDTH);i++) {
        UITableView *tableView=[self.selectAddressScrollView viewWithTag:(self.startTag+i)];
        [self.addressDataMDic removeObjectForKey:@(self.startTag+i)];
        [self removeHeadTitleLabel:@(self.startTag+i)];
        [self.selectIndexPathMDic removeObjectForKey:@(self.startTag+i)];
        [tableView reloadData];
    }
    [self setHeadTitleLabel:@(tag) value:@"请选择"];
    [self.addressDataMDic setObject:infoAry forKey:@(tag)];
    self.selectAddressScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*(tag-self.startTag+1), self.selectAddressScrollView.frame.size.height);
    if([self.selectAddressScrollView viewWithTag:tag]) {
         UITableView *tableView=[self.selectAddressScrollView viewWithTag:tag];
        [tableView reloadData];
    }else {
        [self.selectAddressScrollView addSubview:[self selectAddressTableView:CGRectMake(SCREEN_WIDTH*(tag-self.startTag), 0, SCREEN_WIDTH, self.selectAddressScrollView.frame.size.height) tag:tag]];
    }
    [self.selectAddressScrollView setContentOffset:CGPointMake(SCREEN_WIDTH*(tag-self.startTag), 0) animated:YES];
}

- (void)removeHeadTitleLabel:(id)key {
    [self.headTitleMDic removeObjectForKey:key];
    UILabel *titleLabel=[self.titlesScrollView viewWithTag:[key integerValue]];
    self.titlesScrollView.contentSize=CGSizeMake(self.titlesScrollView.contentSize.width-titleLabel.frame.size.width, self.titlesScrollView.contentSize.height);
    [titleLabel removeFromSuperview];
}

- (void)setHeadTitleLabel:(id)key value:(NSString *)value{
    if([self.headTitleMDic.allKeys containsObject:key]) { //修改
        [self.headTitleMDic setObject:value forKey:key];
        UILabel *titleLabel=[self.titlesScrollView viewWithTag:[key integerValue]];
        titleLabel.text=value;
        titleLabel.textColor=HEXVALEUCOLOR(0x666666, 1.0);
        self.titlesScrollView.contentSize=CGSizeMake(self.titlesScrollView.contentSize.width-titleLabel.frame.size.width, self.titlesScrollView.contentSize.height);
        titleLabel.frame=CGRectMake(titleLabel.frame.origin.x, 0, titleLabel.intrinsicContentSize.width+20, self.titlesScrollView.contentSize.height);
        self.titlesScrollView.contentSize=CGSizeMake(self.titlesScrollView.contentSize.width+titleLabel.intrinsicContentSize.width+20, self.titlesScrollView.contentSize.height);
        [self.titlesScrollView addSubview:titleLabel];
        [self.titlesScrollView addSubview:self.moveLineView];
        self.moveLineView.frame=CGRectMake(titleLabel.frame.origin.x+10, self.titlesScrollView.frame.size.height-2, titleLabel.intrinsicContentSize.width, 2);
        [self moveTitleScrollView:titleLabel.frame];
    }else { //增加
        [self.headTitleMDic setObject:value forKey:key];
        UILabel *titleLabel=[self titleLabel:[key integerValue] text:value];
        titleLabel.frame=CGRectMake(self.titlesScrollView.contentSize.width, 0, titleLabel.intrinsicContentSize.width+20, self.titlesScrollView.contentSize.height);
        self.titlesScrollView.contentSize=CGSizeMake(self.titlesScrollView.contentSize.width+titleLabel.intrinsicContentSize.width+20, self.titlesScrollView.contentSize.height);
        [self.titlesScrollView addSubview:titleLabel];
        [self.titlesScrollView addSubview:self.moveLineView];
        [UIView animateWithDuration:.25 animations:^{
            self.moveLineView.frame=CGRectMake(titleLabel.frame.origin.x+10, self.titlesScrollView.frame.size.height-2, titleLabel.intrinsicContentSize.width, 2);
        }];
        [self moveTitleScrollView:titleLabel.frame];
    }
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *ary = self.addressDataMDic[@(tableView.tag)];
    return ary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"selectAddressTableViewCellReuseID%zd",tableView.tag] forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor=HEXVALEUCOLOR(0x454545, 1.0);
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    if(self.selectIndexPathMDic[@(tableView.tag)]) {
        NSIndexPath *selectIndexPath=self.selectIndexPathMDic[@(tableView.tag)];
        if(indexPath.row==selectIndexPath.row) {
            cell.textLabel.textColor=[UIColor redColor];
        }
    }
    NSArray *ary = self.addressDataMDic[@(tableView.tag)];
    cell.textLabel.text=ary[indexPath.row][@"AREANAME"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *selectIndexPath=self.selectIndexPathMDic[@(tableView.tag)];
    //点击未点击过的cell刷新数据
    if(!selectIndexPath||(selectIndexPath.row!=indexPath.row)) {
        NSArray *ary = self.addressDataMDic[@(tableView.tag)];
        [self setHeadTitleLabel:@(tableView.tag) value:ary[indexPath.row][@"AREANAME"]];
        [self.selectIndexPathMDic setObject:indexPath forKey:@(tableView.tag)];
        [tableView reloadData];
        [self requestData:nil tableViewTag:(tableView.tag+1)];
        return;
    }
    //点击点击过的cell不用刷新数据
    [self.selectAddressScrollView setContentOffset:CGPointMake(SCREEN_WIDTH*(tableView.tag-self.startTag+1), 0) animated:YES];
    UILabel *titleLabel=[self.titlesScrollView viewWithTag:(tableView.tag+1)];
    [UIView animateWithDuration:.25 animations:^{
        self.moveLineView.frame=CGRectMake(titleLabel.frame.origin.x+10, self.titlesScrollView.frame.size.height-2, titleLabel.intrinsicContentSize.width, 2);
    }];
    [self moveTitleScrollView:titleLabel.frame];

}
#pragma mark - scrollViewDelegate
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if(500==scrollView.tag) {
        UILabel *titleLabel=[self.titlesScrollView viewWithTag:((targetContentOffset->x)/SCREEN_WIDTH+self.startTag)];
        [self moveTitleScrollView:titleLabel.frame];
        [UIView animateWithDuration:.25 animations:^{
            self.moveLineView.frame=CGRectMake(titleLabel.frame.origin.x+10, self.titlesScrollView.frame.size.height-2, titleLabel.intrinsicContentSize.width, 2);
        }];
    }
}
 //处理标题视图滚动到可见
- (void)moveTitleScrollView:(CGRect)titleLabelFrame {
    if(CGRectGetMaxX(titleLabelFrame)>(self.titlesScrollView.contentOffset.x+SCREEN_WIDTH)) {
        [self.titlesScrollView setContentOffset:CGPointMake(CGRectGetMaxX(titleLabelFrame)-SCREEN_WIDTH, 0) animated:YES];
        return;
    }else if(CGRectGetMinX(titleLabelFrame)<self.titlesScrollView.contentOffset.x) {
        [self.titlesScrollView setContentOffset:CGPointMake(CGRectGetMinX(titleLabelFrame), 0) animated:YES];
    }
}

#pragma mark -Action
- (void)titleLabelAction:(UITapGestureRecognizer*)tap {
    UILabel *titleLabel=(UILabel *)tap.view;
    [UIView animateWithDuration:.25 animations:^{
        self.moveLineView.frame=CGRectMake(titleLabel.frame.origin.x+10, self.titlesScrollView.frame.size.height-2, titleLabel.intrinsicContentSize.width, 2);
    }];
    [self.selectAddressScrollView setContentOffset:CGPointMake(SCREEN_WIDTH*(titleLabel.tag-self.startTag), 0) animated:YES];
}
#pragma mark - 请求数据
- (void)requestData:(NSDictionary *)paramDic tableViewTag:(NSInteger)tag{
    [self refreshSubviews:tag infoAry:@[
                                        @{
                                            @"AREACODE": @"110100",
                                            @"AREANAME": @"北京市XX"
                                            },
                                        @{
                                            @"AREACODE": @"120100",
                                            @"AREANAME": @"上"
                                            },
                                        @{
                                            @"AREACODE": @"130100",
                                            @"AREANAME": @"南京市ff"
                                            }]];
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容