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

宰父正真
2023-12-01
#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的选中状态


 类似资料: