[一句秒懂]蒙板講解(下)+優化點


關於蒙板哪一套邏輯和思路講解數組

截圖1:-------------------------------------------async

 

截圖2:-------------------------------------------ide

 

 

截圖3:-------------------------------------------優化

 

 

 

上面截圖:--------------------------------------結束截圖lua

 

 

1:首先蒙板那一套咱們可使用一套便可,蒙板外面只要傳一個view控件內容、或者一個控制器:(而後在外面操做全部數據交互效果),關於蒙板的模版講解在上冊已經講過,這裏就再也不多說了spa

2:咱們在外面給蒙板傳一個view或者控制器,咱們須要在view和控制器裏面有一個block回調或者代理,看本身喜歡吧,這樣是爲了:在你談下蒙板的時候,將選擇的值傳給當前控制器線程

3:這時候,你就只要和當前控制器彈出蒙板出來的之後的那個view或者控制器交互數據就能夠了代理

4:三步曲==建立-傳內容-顯示code

5:請求數據==當咱們請求數據的時候,在蒙板裏面選擇數據之後,咱們通常都是將數據傳到外面控制中,經過block或者代理-而後dismiss掉蒙板-而後請求接口(在當前控制器下)component

6:代碼演示:

// 建立
    GoodsDetaiParameterView * goodsDetaiParameterView  = [GoodsDetaiParameterView goodsDetaiParameterView];
    MeSelectDateViewController *vc = [[MeSelectDateViewController alloc]init];
    vc.dataArr = [self.timeData copy];
    vc.model = self.model;  // 傳模型
    vc.myBlock = ^ (NSString *year ,NSString *month, NSString *day) {
        
        YLLog(@"year=%@  month=%@  day=%@",year,month,day);
        [goodsDetaiParameterView dismiss];
        
        [self loadSelectYear:year month:month day:day];
    };
    goodsDetaiParameterView.contentController = vc;
    
    // 顯示
    [goodsDetaiParameterView show];

 

 

額外點金:

1:獲取當前手機端的真實年、月、日

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *myDate = [NSDate date];
    NSString *dateTime = [formatter stringFromDate:myDate];
    
    NSRange range = [dateTime rangeOfString:@"-"];
    if (range.length) {
      self.dateTimeArray = [dateTime componentsSeparatedByString:@"-"];
    }

 

2:根據哪一年哪一月計算多少天數

#pragma mark - 獲取-天數組

- (NSMutableArray *)returnDayArray:(NSUInteger)count {
    [self.dayArray removeAllObjects];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=1; i<=count; i++) {
            [self.dayArray addObject:[NSString stringWithFormat:@"%@",@(i)]];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self selectIndexPathForRow:[self modelWithIndex:self.index]];
        });
    });
    return self.dayArray;
}

#pragma mark - 獲取那一年那一月共有多少天

- (NSUInteger)getNumberOfDaysOneYear:(NSDate *)date{
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSRange range = [calender rangeOfUnit:NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate: date];
    return range.length;
}

#pragma mark - 傳一個月份和一個年份字符串,返回一個NSDate

- (NSDate *)returnDateTimeWithMonthString:(NSString *)monthStr yearString:(NSString *)yearStr {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"M/yyyy"];
    
    // 格式示範:@"2/2035";
    NSString *stringTime = [NSString stringWithFormat:@"%@/%@",monthStr,yearStr];
    return [formatter dateFromString:stringTime];
}

 

3:選中哪一行:必需是刷新後才能看到效果,千萬不能在刷新以前執行下面的代碼

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tempArray indexOfObject:object] inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];

 

自定義選中顏色樣式

UIView *selectedView  = [[UIView alloc]initWithFrame:self.frame];
        selectedView.backgroundColor = [UIColor YLColorBaby];
        self.selectedBackgroundView = selectedView;

 

系統選中顏色樣式:

self.selectionStyle = UITableViewCellSelectionStyleNone;

4:初始化建立多個控件、遍歷

- (instancetype)initWithdataArray:(NSArray *)arr {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, YLScreenW, 101);
        self.backgroundColor = [UIColor YLColorWhite];
        
        if (arr.count==0) {
            arr = [NSArray arrayWithObjects:@"年",@"月",@"日",nil];
        }else {
            NSMutableArray *arr1 = [NSMutableArray array];
            [arr1 addObject:[NSString stringWithFormat:@"%@年",arr[0]]];
            [arr1 addObject:[NSString stringWithFormat:@"%@月",arr[1]]];
            [arr1 addObject:[NSString stringWithFormat:@"%@日",arr[2]]];
            
            arr = [arr1 copy];
        }
        
        CGFloat lblX = 0;
        CGFloat lblY = 50;
        CGFloat lblW = (int)(YLScreenW-lblX*2)/4;
        CGFloat lblH = 50;
        for (int i=0; i<arr.count; i++) {
            UIButton *btn = [[UIButton alloc]init];
            [btn setTitle:arr[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor YLColorGrayPrimary] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor YLColorRedIcon] forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.tag = 10000+i;
            
            
            btn.x = lblX +lblW*i;
            btn.y = lblY;
            btn.width = lblW;
            btn.height = lblH;
            self.dateBtn = btn;
            [self addSubview:btn];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArray addObject:btn];
            
            if (i==0 ) {
                btn.userInteractionEnabled = YES;
            }else {
                btn.userInteractionEnabled = NO;
            }
        }
        
        // 按鈕的選中顏色
        UIButton *firstButton = self.subviews.firstObject;
        
        UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_list"]];
        imgV.width = 45;
        imgV.height = 3;
        imgV.centerX = firstButton.centerX;
        imgV.y = self.height - imgV.height;
        [self addSubview:imgV];
        self.imageView = imgV;
        
        // 自動計算好第一個按鈕的文字寬度
        [firstButton.titleLabel sizeToFit];
        // 默認:選中第一個按鈕
        [self btnClick:firstButton];
        
        [self setupUI];
    }
    return self;
}

 

5:若是咱們想要拿到剛開始初始化一次性建立的多個按鈕,咱們能夠將其放在一個數組中保存起來,經過不一樣下標取出裏面的按鈕,就能夠改變每個按鈕的內容了,

#pragma mark - 外部修改方法

- (void)configBtn:(NSInteger)index title:(NSString *)title {
    NSString *newTitle = nil;
    switch (index-1) {
        case 0:
            newTitle = [NSString stringWithFormat:@"%@年",title];
            break;
        case 1:
            newTitle = [NSString stringWithFormat:@"%@月",title];
            break;
            
        default:
            break;
    }
    
    
    UIButton *btn = self.btnArray[index-1];
    [btn setTitle:newTitle forState:UIControlStateNormal];
    
    UIButton *btn1 = self.btnArray[index];
    [self changeButtonStatus:btn1];
    
    for (int i=0; i<=index; i++) {
        UIButton *button = self.btnArray[i];
        button.userInteractionEnabled = YES;
    }
}

6:一排按鈕的點擊狀態修改:

#pragma mark - 公用方法提取出來

- (void)changeButtonStatus:(UIButton *)sender {
    // 設置當前按鈕的狀態
    self.dateBtn.selected = NO;
    // 設置傳入的按鈕爲選中狀態
    sender.selected = YES;
    // 設置當前按鈕爲傳入的按鈕
    self.dateBtn = sender;
    
    // 指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.imageView.width = 45;  //sender.titleLabel.width;
        self.imageView.centerX = sender.centerX;
    }];
}

 

7:優化處理:

後期我將全部的數據都是經過模型去修改,也就是說在這裏面的年、月、日都是經過模型傳來傳去,在模型裏面操做數據的根本,很方便

/**
 *  時間戳轉換爲時間
 */
- (NSString *)ctime {
    
    NSTimeInterval interTime = [_ctime doubleValue] + 0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interTime];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    //[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *str = [formatter stringFromDate:date];
    return str;
}

 

8:---判斷年月日數據爲空或者其餘類型的時候,還有不能爲0的字符串的判斷---

#import "YLStrTool.h"

@implementation YLStrTool


+ (BOOL)isEmpty:(NSString *)str{
    if(str.length<=0 || str==nil ||[str isKindOfClass:[NSNull class]]){
        return YES;
    }
    return NO;
}


#pragma mark - 是否合法

- (BOOL)islegal:(NSString *)str {

    if ([YLStrTool isEmpty:str]) {  //是空的
        return YES;
    }else { //不是空的
        if ([str isEqualToString:@"0"]) {
            return YES;
        }else {
            return NO;
        }
    }
}

 

9:---這兩個get方法都試模型數據的get方法:目的是給模型數據從新加工處理之後,才輸出給咱們,好比說06月->6月---

/**
 * 判斷06月 -> 6月
 */

- (NSString *)b_month {
    if ([_b_month hasPrefix:@"0"]) {
        return [_b_month substringFromIndex:1];
    }else {
        return _b_month;
    }
}


- (NSString *)b_day {
    if ([_b_day hasPrefix:@"0"]) {
        return [_b_day substringFromIndex:1];
    }else {
        return _b_day;
    }
}

 

10-將6月-6,正則匹配而後進行裁剪

#pragma mark -  數字和中文組合的字符串:提取純數字 // self爲當前字符串

- (NSString *)stringToNumberStr {
    NSString * regex = @"^[0-9]*$";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:self]) {  //是純數字
        return self;
    } else {  // 不是純數字
        return [self substringToIndex:self.length-1];
    }
    return nil;
}

 

11-遍歷數組for循環的時候,最好是開啓一個子線程-如

dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=0; i<distanceValue; i++) {
            long originDate = [dateTime longLongValue];
            long newDate = originDate - i;
            [self.yearArray insertObject:[NSString stringWithFormat:@"%@",@(newDate)] atIndex:0];
        }
        
        NSMutableArray *latestDateArray = [NSMutableArray array];
        for (int j=distanceValue; j>0; j--) {
            long originDate = [dateTime longLongValue];
            long newDate = originDate + j;
            [latestDateArray insertObject:[NSString stringWithFormat:@"%@",@(newDate)] atIndex:0];
        }
        
        [self.yearArray addObjectsFromArray:latestDateArray];
        self.tempArray = self.yearArray;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            
              [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tempArray indexOfObject:[self modelWithIndex:self.index]] inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
        });
    });
相關文章
相關標籤/搜索