UITableView 選中單元格 全都可以選


每個單元格都可選中, 只選擇單個單元格


//
//  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;
    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:@"tick"] forState:UIControlStateSelected];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(height/2 - 10, width/2 - 10, height/2-10, width/2-10)];
            [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];
            NSArray *subView = btn.subviews;
            if ([cellInfo[i] intValue] == 0) {
                
                if (subView) {
                    
                    for (UIView *view in subView) {
                        
                        if (view.tag == 901) {
                            [view removeFromSuperview];

                        }
                    }
                    
                }
                
            }else{
                
                UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(width*2/3, 0, width/3, height/3)];
                imgV.tag = 901;
                imgV.image = [UIImage imageNamed:@"clinic1"];
                [btn addSubview:imgV];

            }
        }
        
        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


發佈了68 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章