|
//
// nodeInfoView.m
// ThePaperDemo
//
// Created by scar1900 on 14-9-23.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "nodeInfoViewCell.h"
#import "AsyncImageView.h"
@interface nodeInfoViewCell() {
CGFloat contentPadding;
}
@property(nonatomic, strong)AsyncImageView *sponsorImageView;
@property(nonatomic, strong)AsyncImageView *nodeImageView;
@property(nonatomic, strong)UIButton *nodeButton;
@property(nonatomic, strong)UIButton *orderButton;
@property(nonatomic, strong)UILabel *nodeLabel;
@end
@implementation nodeInfoViewCell
@synthesize nodeInfoBO = _nodeInfoBO;
@synthesize delegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
if (IS_IPHONE_6P) {
contentPadding = 15;
}else if (IS_IPHONE_6) {
contentPadding = 15;
}else {
contentPadding = 10;
}
self.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.sponsorImageView];
[self.contentView addSubview:self.nodeImageView];
[self.contentView addSubview:self.nodeButton];
[self.contentView addSubview:self.orderButton];
[self.contentView addSubview:self.nodeLabel];
for (int i=0; i<2; i++) {
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(contentPadding, 1+i*(40-2), rect_screen.size.width-2*contentPadding, 1)];
lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
lineView.tag = 100+i;
[self.contentView addSubview:lineView];
}
[self setLayoutSubviews];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
return self;
}
- (void)needrefreshNightMode:(id)sender{
// [_nodeButton setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
// [_nodeButton setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateHighlighted];
// _orderButton.backgroundColor = [UIColor colorWithHexString:LIGHTGRAY];
[_orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:LIGHTGRAY]) forState:UIControlStateNormal];
[_orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) forState:UIControlStateSelected];
_nodeLabel.textColor = [UIColor colorWithHexString:TextBlack];
UIView *line1 = [self.contentView viewWithTag:100];
UIView *line2 = [self.contentView viewWithTag:101];
line1.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
line2.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (AsyncImageView*)sponsorImageView {
if (!_sponsorImageView) {
_sponsorImageView = [[AsyncImageView alloc]initWithFrame:CGRectZero];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
_sponsorImageView.userInteractionEnabled = YES;
[_sponsorImageView addGestureRecognizer:tap];
_sponsorImageView.isHaveWaterPrint = NO;
}
return _sponsorImageView;
}
- (AsyncImageView*)nodeImageView {
if (!_nodeImageView) {
_nodeImageView = [[AsyncImageView alloc]initWithFrame:CGRectZero];
_nodeImageView.isHaveWaterPrint = NO;
}
return _nodeImageView;
}
- (UIButton*)nodeButton {
if (!_nodeButton) {
_nodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
// [_nodeButton setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
// [_nodeButton setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateHighlighted];
// _nodeButton.titleLabel.backgroundColor = [UIColor redColor];
// _nodeButton.titleLabel.textAlignment = NSTextAlignmentRight;
[_nodeButton addTarget:self action:@selector(pushToNodeList:) forControlEvents:UIControlEventTouchUpInside];
}
_nodeButton.titleLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
return _nodeButton;
}
- (UILabel *)nodeLabel {
if (nil == _nodeLabel) {
_nodeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nodeLabel.textColor = [UIColor colorWithHexString:TextBlack];
_nodeLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_nodeLabel.textAlignment = NSTextAlignmentLeft;
}
return _nodeLabel;
}
- (void)setNodeInfoBO:(nodeObjectBO *)data {
if (_nodeInfoBO == data) {
return;
}
_nodeInfoBO = data;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *sponsorUrl = @"";
NSString *sponsorId = @"";
if (data.sponsor) {
sponsorBO *sponsor = setJsonDicToDataModel(data.sponsor, [sponsorBO class]);
sponsorUrl = sponsor.logo;
sponsorId = getImageNameFromURL(sponsor.logo);
}
NSString *imageUrl = data.pic;
NSString *imageID = getImageNameFromURL(data.pic);
dispatch_async(dispatch_get_main_queue(), ^{
if (data.sponsor) {
self.sponsorImageView.imageUrl = sponsorUrl;
self.sponsorImageView.imageId = sponsorId;
}
self.nodeImageView.imageUrl = imageUrl;
self.nodeImageView.imageId = imageID;
self.nodeLabel.text = data.name;
// [self.nodeButton setTitle:data.name forState:UIControlStateNormal];
if ([data.isOrder intValue] == 0) {
self.orderButton.selected = NO;
}else {
self.orderButton.selected = YES;
}
[self relayoutSubviews];
});
});
}
- (UIButton*)orderButton {
if (!_orderButton) {
_orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
_orderButton.layer.cornerRadius = 3;
[_orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:LIGHTGRAY]) forState:UIControlStateNormal];
[_orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) forState:UIControlStateSelected];
[_orderButton addTarget:self action:@selector(orderHandler:) forControlEvents:UIControlEventTouchUpInside];
[_orderButton setImage:Image(@"detailPage/addIcon.png") forState:UIControlStateNormal];
[_orderButton setImage:Image(@"detailPage/ordered.png") forState:UIControlStateSelected];
[_orderButton setTitle:@"订阅" forState:UIControlStateNormal];
[_orderButton setTitle:@"已订阅" forState:UIControlStateSelected];
_orderButton.titleLabel.textAlignment = NSTextAlignmentLeft;
_orderButton.titleLabel.textColor = [UIColor clearColor];
}
_orderButton.titleLabel.font = appFont(TEXT_SEVEN_LEVELSIZE, NO);
return _orderButton;
}
- (void)orderHandler:(UIButton*)btn {
if ([self.delegate respondsToSelector:@selector(orderClick:nodeInfo:)]) {
[self.delegate orderClick:btn nodeInfo:self.nodeInfoBO];
}
}
- (void)tap:(id)sender {
if ([self.delegate respondsToSelector:@selector(tapNodeSporWithNodeInfo:)]) {
[self.delegate tapNodeSporWithNodeInfo:self.nodeInfoBO];
}
}
- (void)setLayoutSubviews {
CGFloat nodeButtonWidth =50;
// self.nodeButton.frame =CGRectMake(CGRectGetMaxX(self.nodeImageView.frame)+10,
// CGRectGetHeight(self.bounds)/2-10,
// nodeButtonWidth,
// 20);
[self.sponsorImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.left).offset(contentPadding);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@0);
make.height.equalTo(@0);
}];
[self.nodeImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.sponsorImageView.right);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@20);
make.height.equalTo(@20);
}];
[self.nodeButton makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.nodeImageView.left);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@(nodeButtonWidth));
make.height.equalTo(@20);
}];
[self.nodeLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.nodeImageView.right).offset(22.5);
make.top.equalTo(self.contentView.top).offset(12);
make.height.mas_equalTo(16);
make.width.mas_equalTo(rect_screen.size.width-120);
}];
[self.orderButton makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView.top).offset(10);
make.right.equalTo(self.contentView.right).offset(-contentPadding);
make.width.mas_equalTo(54);
make.height.equalTo(@20);
}];
self.orderButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 50-5-15/2);
[self.orderButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
}
- (void)relayoutSubviews {
CGFloat nodeButtonWidth = widthForString(self.nodeInfoBO.name, self.nodeButton.titleLabel.font, 20, NSLineBreakByWordWrapping)+20+30;
if (self.nodeInfoBO.sponsor) {
[self.sponsorImageView remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.left).offset(contentPadding);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@80);
make.height.equalTo(@20);
}];
[self.nodeImageView remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.sponsorImageView.right).offset(10);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@20);
make.height.equalTo(@20);
}];
}else{
[self.sponsorImageView remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.left).offset(contentPadding);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@0);
make.height.equalTo(@0);
}];
[self.nodeImageView remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.sponsorImageView.right);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@20);
make.height.equalTo(@20);
}];
}
[self.nodeButton remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.nodeImageView);
make.centerY.equalTo(self.contentView.centerY);
make.width.equalTo(@(nodeButtonWidth));
make.height.equalTo(@20);
}];
}
- (void)pushToNodeList:(UIButton*)btn {
if ([self.delegate respondsToSelector:@selector(goToNodeListPage:)]) {
[self.delegate goToNodeListPage:self.nodeInfoBO];
}
}
@end
|