// // DRTimeLineChannelCell.m // YouHuiProject // // Created by 小花 on 2019/1/3. // Copyright © 2019年 kuxuan. All rights reserved. // #import "DRTimeLineChannelCell.h" #import "DRLoginViewController.h" @interface DRTimeLineChannelCell () @property (nonatomic, strong) UIImageView *iconView; @property (nonatomic, strong) UILabel *title; @property (nonatomic, strong) UIButton *signBtn; @end @implementation DRTimeLineChannelCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; } return self; } - (void)initSubViews { [self.contentView addSubview:self.iconView]; [self.contentView addSubview:self.title]; [self.contentView addSubview:self.signBtn]; [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(10); make.centerX.mas_equalTo(self.mas_centerX); make.width.height.mas_equalTo(48); }]; [self.title mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.iconView.mas_bottom).mas_offset(10); make.centerX.mas_equalTo(self.iconView.mas_centerX); make.width.mas_equalTo(80); }]; [self.signBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.title.mas_bottom).mas_offset(13); make.width.mas_equalTo(50); make.height.mas_equalTo(18); make.centerX.mas_equalTo(self.title.mas_centerX); }]; } - (void)setModel:(DRTimeLineChannelModel *)model { _model = model; [self.iconView sd_setFadeImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil options:0 progress:nil completed:nil]; self.title.text = model.title; self.signBtn.selected = model.status.boolValue; } - (void)signAction:(UIButton *)sender { if (![AccountTool isLogin]) { DRLoginViewController *login = [[DRLoginViewController alloc] init]; [[self currentViewController] presentViewController:login animated:YES completion:nil]; return; } sender.enabled = NO; NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/subscribeLabel",BaseURL]; NSNumber *status = sender.selected?@(0):@(1); NSDictionary *para = @{@"label_id":self.model.Id,@"status":status}; [DRHttp post:url params:para success:^(id json) { NSNumber *flag = json[@"flag"]; if (flag.boolValue) { sender.selected = !sender.selected; } [XHToast showCenterWithText:json[@"msg"]]; sender.enabled = YES; } failure:^(NSError *error) { sender.enabled = YES; }]; [MobClick event:OptimizingCircleLabelClick label:self.model.title]; } #pragma mark ----- - (UIImageView *)iconView { if (!_iconView) { _iconView = [[UIImageView alloc] init]; _iconView.layer.cornerRadius = 24; _iconView.layer.masksToBounds = YES; _iconView.backgroundColor = [UIColor yhGrayColor]; } return _iconView; } - (UILabel *)title { if (!_title) { _title = [[UILabel alloc] init]; _title.font = [UIFont systemFontOfSize:14]; _title.textColor = [UIColor YHColorWithHex:0x4B4B4B]; _title.textAlignment = NSTextAlignmentCenter; } return _title; } - (UIButton *)signBtn { if (!_signBtn) { _signBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_signBtn setImage:[UIImage imageNamed:@"sign_n"] forState:UIControlStateNormal]; [_signBtn setImage:[UIImage imageNamed:@"signed"] forState:UIControlStateSelected]; [_signBtn addTarget:self action:@selector(signAction:) forControlEvents:UIControlEventTouchUpInside]; } return _signBtn; } - (UIViewController *)currentViewController{ UIViewController * currVC = nil; UIWindow *window = [UIApplication sharedApplication].delegate.window; UIViewController * Rootvc = window.rootViewController; do { if ([Rootvc isKindOfClass:[UINavigationController class]]) { UINavigationController * nav = (UINavigationController *)Rootvc; UIViewController * v = [nav.viewControllers lastObject]; currVC = v; Rootvc = v.presentedViewController; continue; }else if([Rootvc isKindOfClass:[UITabBarController class]]){ UITabBarController * tabVC = (UITabBarController *)Rootvc; currVC = tabVC; Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex]; continue; } } while (Rootvc!=nil); return currVC; } @end