京東選取地址小控件

這東西實(shí)現(xiàn)起來并不是很難闻蛀,就是相對(duì)來說啰嗦一點(diǎn)仙蛉,好了廢話不多說 直接上效果

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

具體效果可以參考京東選取地址的效果哦,由于我不經(jīng)常發(fā)表簡書冠息,就直接上圖片了挪凑,沒搞gif。

代碼思路:

1.視圖結(jié)構(gòu):標(biāo)題scrollview+內(nèi)容scrollview(n個(gè)tableview展示數(shù)據(jù))

2.主要邏輯:上下scrollview的聯(lián)動(dòng)铐达,點(diǎn)擊新數(shù)據(jù)時(shí)生產(chǎn)tableview(有時(shí)會(huì)重用)+titleLabel岖赋,內(nèi)容偏移的處理

哦了,下面直接上代碼了瓮孙,不瞎bb,如果有任何bug和優(yōu)化的可以@我哦唐断,多謝了,喜歡的話可以點(diǎn)點(diǎn)贊哦

//
//  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=@"所在地區(qū)";
    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);
    }];
}

//首次創(chuàng)建時(shí)增加tableview
- (void)firstAddTableViews {
    [self requestData:nil tableViewTag:self.startTag];
}
//刷新tableview和標(biāo)題欄
- (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)];
    //點(diǎn)擊未點(diǎn)擊過的cell刷新數(shù)據(jù)
    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;
    }
    //點(diǎn)擊點(diǎn)擊過的cell不用刷新數(shù)據(jù)
    [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);
        }];
    }
}
 //處理標(biāo)題視圖滾動(dòng)到可見
- (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 - 請求數(shù)據(jù)
- (void)requestData:(NSDictionary *)paramDic tableViewTag:(NSInteger)tag{
    [self refreshSubviews:tag infoAry:@[
                                        @{
                                            @"AREACODE": @"110100",
                                            @"AREANAME": @"北京市XX"
                                            },
                                        @{
                                            @"AREACODE": @"120100",
                                            @"AREANAME": @"上"
                                            },
                                        @{
                                            @"AREACODE": @"130100",
                                            @"AREANAME": @"南京市ff"
                                            }]];
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末杭抠,一起剝皮案震驚了整個(gè)濱河市脸甘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌偏灿,老刑警劉巖丹诀,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡铆遭,警方通過查閱死者的電腦和手機(jī)硝桩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枚荣,“玉大人碗脊,你說我怎么就攤上這事¢献保” “怎么了衙伶?”我有些...
    開封第一講書人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長害碾。 經(jīng)常有香客問我矢劲,道長,這世上最難降的妖魔是什么慌随? 我笑而不...
    開封第一講書人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任芬沉,我火速辦了婚禮,結(jié)果婚禮上阁猜,老公的妹妹穿的比我還像新娘花嘶。我一直安慰自己,他們只是感情好蹦漠,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開白布椭员。 她就那樣靜靜地躺著,像睡著了一般笛园。 火紅的嫁衣襯著肌膚如雪隘击。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,031評(píng)論 1 285
  • 那天研铆,我揣著相機(jī)與錄音埋同,去河邊找鬼。 笑死棵红,一個(gè)胖子當(dāng)著我的面吹牛凶赁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逆甜,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼虱肄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了交煞?” 一聲冷哼從身側(cè)響起咏窿,我...
    開封第一講書人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎素征,沒想到半個(gè)月后集嵌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萝挤,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年根欧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了怜珍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凤粗,死狀恐怖绘面,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情侈沪,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布晚凿,位于F島的核電站亭罪,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏歼秽。R本人自食惡果不足惜应役,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望燥筷。 院中可真熱鬧箩祥,春花似錦、人聲如沸肆氓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谢揪。三九已至蕉陋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拨扶,已是汗流浹背凳鬓。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留患民,地道東北人缩举。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像匹颤,于是被迫代替她去往敵國和親仅孩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容