如何让tableviewcell在拖动过后保持选中状态,避免重用机制的影响

#import "AlarmManageViewController.h"
#import "Tools.h"
#import "UIImage+ChangeSize.h"
@interface AlarmManageViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIDatePicker * timePicker;
    UITableView * repeatTableView;
    NSMutableArray * weekdays;
    NSMutableArray * SelectionArr;
}
@end

@implementation AlarmManageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [Tools setNavImgForViewController:self];
    [self createBackButton];
	// Do any additional setup after loading the view.
    timePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 216.0)];
    timePicker.datePickerMode = UIDatePickerModeTime;
    [self.view addSubview:timePicker];
    
    weekdays = [[NSMutableArray alloc]initWithObjects:@"Never",@"Monday",@"Tuesday",@"Wednesday",
    @"Thursday",@"Friday",@"Saturday",@"Sunday",nil];
    SelectionArr =[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],nil];
    
//    NSLog(@"%f",CGRectGetHeight(timePicker.frame));
    repeatTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 216, screenWidth, screenHeight -216-20-44) style:UITableViewStyleGrouped];
    repeatTableView.delegate = self;
    repeatTableView.dataSource = self;
    [self.view addSubview:repeatTableView];
    
}

-(void)createBackButton
{
    self.navigationController.navigationBarHidden = NO;
	//创建back按钮取代自动产生的后退按钮
	self.navigationItem.hidesBackButton = YES;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 25.0, 25.0);
    [button setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = backBarButton;
     
     UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
     right.frame = CGRectMake(0, 0, 50.0, 50.0);
    [right setImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
      [right addTarget:self action:@selector(saveToFile) forControlEvents:UIControlEventTouchUpInside];
      UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:right];
      self.navigationItem.rightBarButtonItem = rightButton;
}
-(void)back
{
      [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}

#define mark - tableview delegate datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[[UITableViewCell alloc]init];
    
    NSInteger row =[indexPath row];
    cell.textLabel.text =[weekdays objectAtIndex:row];
    if ([[SelectionArr objectAtIndex:row] boolValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row =[indexPath row];
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    NSIndexPath * neverIndexpath =[NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell * neverCell = [tableView cellForRowAtIndexPath:neverIndexpath];
    if (row!=0) {       //选中weekday则取消never选中
       if (neverCell.accessoryType == UITableViewCellAccessoryCheckmark) neverCell.accessoryType = UITableViewCellAccessoryNone;
        [SelectionArr replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:NO]];
        //切换cell的选中状态
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            cell.accessoryType =UITableViewCellAccessoryNone;
            [SelectionArr replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:NO]];

        }else if (cell.accessoryType == UITableViewCellAccessoryNone)
        {
            cell.accessoryType =UITableViewCellAccessoryCheckmark;
            [SelectionArr replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:YES]];
        }
    }else
    {   //选中never则weekday全部取消选中
        if (neverCell.accessoryType == UITableViewCellAccessoryNone) {
            neverCell.accessoryType = UITableViewCellAccessoryCheckmark;
             [SelectionArr replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:YES]];
            [self resetWeekDaysSelection];
            [tableView reloadData];//重置界面选中状态
        }
        
    }
}

-(void)resetWeekDaysSelection
{
    for (int i=1; i<8; i++) {
        [SelectionArr replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
    }
}

@end

这个过程,主要是用selectionArr数组保存每个cell的选中状态


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值