您的位置:首页 > 其它

cell 复用问题 cell中控件单选,多选,带标签图的不可选中

2015-02-02 11:33 567 查看
带标签图的不可选中



//
//  AddVisitTimeVC.h
//  com.yx129.yxClientDoctor3
//
//  Created by yx on 15/1/30.
//  Copyright (c) 2015年 Guangzhou Yixiang Internet Technology Development Limited. All rights reserved.
//

#import "YXBaseController.h"

@interface AddVisitTimeVC : YXBaseController

@property (nonatomic,copy) void (^backAddTimeText)(NSString *timeText);

@end


//
//  AddVisitTimeVC.m
//  com.yx129.yxClientDoctor3
//
//  Created by yx on 15/1/30.
//  Copyright (c) 2015年 Guangzhou Yixiang Internet Technology Development Limited. All rights reserved.
//

#define cellContentOriginTag  301

#define CurCellHeight  (CGRectGetHeight(_tableView.frame)/7.0)

#define TopHeight 70

#define kSliderWidth 0.5
//
//#define kCellSliderWidth 0.2

#import "AddVisitTimeVC.h"

@interface AddVisitTimeVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation AddVisitTimeVC
{
UITableView *_tableView;
NSMutableArray *_visitTimeArray;

UIButton *_selectedBtn;
int indexRow;

NSDateFormatter *_formatter;
NSDateFormatter *_formatter2;
//    NSDateFormatter *
NSCalendar *_curCalendar;
NSDate *_curDate;
}

-(id)init
{
if (self = [super init]) {
_visitTimeArray = [[NSMutableArray alloc] init];
_formatter = [[NSDateFormatter alloc] init];
[_formatter setDateFormat:@"MM/dd:E"];
//        [_formatter2 setDateFormat:@"F"];
_curCalendar =  [NSCalendar currentCalendar];

}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = kViewBackColor;
[_navView setTitle:@"加号" type:NavTypeBackAndOK];

[self prepareView];
[self refreshData];
}

-(void)onClickLeftBtn
{
[self.navigationController popViewControllerAnimated:YES];
}

-(void)prepareView
{

float topHeight = (TopHeight - 2*kSliderWidth);
UILabel * tipsLable = [[UILabel alloc]initWithFrame:CGRectMake(0, kNavBottomY + 1, IPHONE_WIDTH, topHeight*3/7)];
tipsLable.font = kLableFont;
tipsLable.textColor = HEXCOLOR(0x625d42);
tipsLable.textAlignment = NSTextAlignmentCenter;
tipsLable.text = @"请勾选加号时间";
tipsLable.backgroundColor = HEXCOLORAL(0xfffef5, 0.9);
[self.view addSubview:tipsLable];

NSArray *titlesArray = @[@"",@"上午",@"下午",@"晚上"];
float y = CGRectGetMaxY(tipsLable.frame) + kSliderWidth;
float width = IPHONE_WIDTH/4;
UILabel *lable;
for (int i = 0; i < 4; i++) {
lable = [[UILabel alloc] initWithFrame:CGRectMake(width*i, y, width, topHeight*4/7)];
lable.backgroundColor = [UIColor whiteColor];
lable.textAlignment = NSTextAlignmentCenter;
lable.text = titlesArray[i];
lable.font = kTitleFont;
[self.view addSubview:lable];

}

y = CGRectGetMaxY(lable.frame) + kSliderWidth;
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, y, IPHONE_WIDTH, CGRectGetMaxY(self.view.frame) - y) style:UITableViewStylePlain];
_tableView.layer.borderWidth = 0.2;
_tableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.showsVerticalScrollIndicator = NO;
[self.view addSubview:_tableView];

}

- (void)refreshData{

kShowNetworkActivityIndicator(YES);
[self.hudManager With_label:@"请稍后 " duration:10];

NSMutableDictionary * paramsDic = [[NSMutableDictionary alloc]init];
[paramsDic setObject:@"Sourceclient" forKey:@"module"];
[paramsDic setObject:@"getVisitTime" forKey:@"action"];
[paramsDic setObject:@"Doctor" forKey:@"controller"];

[paramsDic setObject:kSecret forKey:@"secret"];
[paramsDic setObject:kSharedDoctor.token_key forKey:@"token_key"];
[paramsDic setObject:kSharedDoctor.acc_id forKey:@"acc_id"];

YXHttpRequest * request = [YXHttpRequest requestWithParams:paramsDic completion:^(NSDictionary *result,NSError * error){

kShowNetworkActivityIndicator(NO);
[self.hudManager hide];

if (!error) {
NSInteger code = [[result valueForKey:@"code"] integerValue];
NSString * msg = [result valueForKey:@"msg"];
NSArray * listArray = [result valueForKey:@"list"];
if ([listArray isKindOfClass:[NSArray class]]) {

[_visitTimeArray removeAllObjects];
[_visitTimeArray addObjectsFromArray:listArray];
[_tableView reloadData];

}else{

}

}else{
[request showErrorMessage];
}}];
}

#pragma mark -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 28;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return CurCellHeight;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

if (!_selectedBtn) {
return;
}

UIButton *btn;

NSArray *cellArray = cell.contentView.subviews;

for (UIView *view in cell.contentView.subviews) {

if ([view isKindOfClass:[UIButton class]]) {
btn = (UIButton *)view;
btn.selected = NO;

if (indexPath.row == indexRow && [btn isEqual:_selectedBtn]) {
btn.selected = YES;
}

}

}
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

float width = IPHONE_WIDTH/4.0; //(IPHONE_WIDTH - kCellSliderWidth*3)/4.0;
float height = CurCellHeight ;

UILabel *dateLabel1;
UILabel *dateLabel2;
UIButton *btn1;
UIButton *btn2;
UIButton *btn3;

//    cell.backgroundColor = [UIColor whiteColor];
//    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    cell.layer.borderWidth = kCellSliderWidth;

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *bgView = [[UIImageView alloc] initWithFrame:cell.bounds];
bgView.contentMode = UIViewContentModeScaleToFill;
NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"add_visitime_bg" ofType:@"png"];
bgView.image = [UIImage imageNamed:imgPath];
cell.backgroundView = bgView;

dateLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 1 + height/4, width, height/4)];
dateLabel1.backgroundColor = [UIColor clearColor];
dateLabel1.textAlignment = NSTextAlignmentCenter;
//        dateLabel1.text = @"12/03";
dateLabel1.font = kLableFont;
[cell.contentView addSubview:dateLabel1];

dateLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, CurCellHeight/2, width, height/4)];
dateLabel2.backgroundColor = [UIColor clearColor];
dateLabel2.textAlignment = NSTextAlignmentCenter;
//        dateLabel2.text = @"星期一";
dateLabel2.font = kLableFont;
[cell.contentView addSubview:dateLabel2];

dateLabel1.tag = cellContentOriginTag;
dateLabel2.tag = cellContentOriginTag + 1;

for (int i = 0; i < 3; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(width*(i+1), 0, width, height);
btn.backgroundColor = [UIColor clearColor];
[btn setBackgroundImage:[UIImage imageWithColor:kViewBackColor] forState:UIControlStateHighlighted];
[btn setImage:[UIImage imageNamed:@"clinic"] forState:UIControlStateDisabled];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, width*2/3, height*2/3, 0)];

[btn setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateSelected];

[cell.contentView addSubview:btn];

btn.tag = cellContentOriginTag+2 + i;
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
}

}

if (_visitTimeArray.count > indexPath.row%7) {

NSArray *cellInfo = (NSArray *)_visitTimeArray[indexPath.row%7];

for (int i = 0; i < 3; i++) {

UIButton *btn = [cell.contentView viewWithTag:cellContentOriginTag+2 + i];

if ([cellInfo[i] intValue] == 0) {
btn.enabled = YES;
[btn setImageEdgeInsets:UIEdgeInsetsMake(height/2 - 10, width/2 - 10, height/2-10, width/2-10)];

}else{
btn.enabled = NO;//size 20,20
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, width*2/3, height*2/3, 0)];
}
}

dateLabel1 = [cell.contentView viewWithTag:cellContentOriginTag];
dateLabel2 = [cell.contentView viewWithTag:cellContentOriginTag + 1];

NSDate *cellDay = [[NSDate date]dateByAddingTimeInterval:24*60*60*indexPath.row];
NSMutableString *_dateStr = [_formatter stringFromDate:cellDay];

dateLabel1.text = [[_dateStr componentsSeparatedByString:@":"] firstObject];  //[_formatter stringFromDate:_curDate];

dateLabel2.text = [self reverseWeekString:[[_dateStr componentsSeparatedByString:@":"] lastObject]];

}

return cell;

}

-(NSString *)reverseWeekString:(NSString *)str
{
NSString *weekStr;

if ([str hasPrefix:@"Mon"]) {
weekStr = @"星期一";
}else if ([str hasPrefix:@"Tue"]) {
weekStr = @"星期二";
}else if ([str hasPrefix:@"Wed"]) {
weekStr = @"星期三";
}else if ([str hasPrefix:@"Thu"]) {
weekStr = @"星期四";
}else if ([str hasPrefix:@"Fir"]) {
weekStr = @"星期五";
}else if ([str hasPrefix:@"Sat"]) {
weekStr = @"星期六";
}else if ([str hasPrefix:@"Sun"]) {
weekStr = @"星期日";
}

return weekStr;
}

-(void)btnClicked:(UIButton *)button
{
if (_selectedBtn) {
_selectedBtn.selected = NO;
}

button.selected = YES;

UITableViewCell *cell = button.superview.superview;

indexRow = [_tableView indexPathForCell:cell].row;
_selectedBtn = button;

}

-(void)onClickRightBtn
{
[_formatter setDateFormat:@"yyyy年MM月dd日"];
NSMutableString *dateStr = [NSMutableString stringWithString:[_formatter stringFromDate:[[NSDate date] dateByAddingTimeInterval:24*60*60*indexRow]]];

switch (_selectedBtn.tag - (cellContentOriginTag+2)) {
case 0:
{
[dateStr appendString:@"上午"];
}
break;
case 1:
{
[dateStr appendString:@"下午"];
}
break;

case 2:
{
[dateStr appendString:@"晚上"];
}
break;

default:
break;
}

self.backAddTimeText(dateStr);
[self.navigationController popViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐