用UITableView实现单选(打对勾)


若是有更好的方法,但愿各位不吝赐教 ide

@interface SetRemindCycleViewController ()<UITableViewDataSource,UITableViewDelegate> atom

{ spa

    NSIndexPath *current; .net

    NSInteger recordRow; token

    BOOL isSetFail; get

} it

//自定义TableView io

@property (nonatomic, strong) UITableView *customTableView; table

@property (nonatomic, strong) NSArray *cycleArr; 变量

@end


@implementation SFSetRemindCycleViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.title = @"提醒设置";

    self.customTableView=[PublicMethod initTableviewOnSuperview:self.view target:self WithFrame:CGRectMake(0, 0, kScreenViewWidth, K_NORMAL_VIEW_HEIGHT) style:UITableViewStyleGrouped backgroundColor:SFTabelViewBackGroundColor];

    self.customTableView.scrollEnabled=NO;

    

}


-(NSArray *)cycleArr

{

    if (!_cycleArr) {

        _cycleArr = [NSArray arrayWithObjects:@"实时提醒",@"2小时提醒一次", nil];

    }

    return _cycleArr;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.cycleArr.count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 15.0f;

}

//自定义tableView的headerView

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIView *headerView = [[UIView alloc] init];

    headerView.backgroundColor = kBackgroundColor;

    return headerView;

    

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *identifier = @"setRemindCycle";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

        

    }

    if(indexPath.row == [[[NSUserDefaults standardUserDefaults] objectForKey:kSetTodoRemindCycle] integerValue])

    {

        recordRow = indexPath.row;

//current全局变量用来记录没有进行设置以前的选择的cell

        current = indexPath;

//设置cell的属性accessoryType,实现打对勾的效果

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else

    {

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.textLabel.text = self.cycleArr[indexPath.row];

    return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row != current.row) {

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:current];

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

//recordRow全局变量用来防止点击已选择的cell,重复请求数据

    if (indexPath.row != recordRow) {

        recordRow = indexPath.row;

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

//isSetFail全局变量用来记录设置成功或失败

        if (!isSetFail) {

            [self showWaitingHUD];

            [[DataCenter sharedDataCenter] setTodoRemindCycleWithUserID:[UserItem sharedInstance].userID

                                                                  token:[UserItem sharedInstance].token

                                                                cyctype:indexPath.row

                                                                 target:self

                                                             successSEL:@selector(sendRequestTodoRemindCycleSuccesswithItem:)

                                                                failSEL:@selector(sendRequestTodoRemindCycleFailwithItem:)];

        }


    }

        

}


-(void)sendRequestTodoRemindCycleSuccesswithItem:(ResponseItem *)responseItem

{

//若是设置成功,则将设置信息保存到本地,并调用block回调刷新上个界面

    NSIndexPath *indexPath = [self.customTableView indexPathForSelectedRow];

    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:indexPath.row] forKey:kSetTodoRemindCycle];

    self.setRemindCycleBlock();

    [self hideWaitingHUD];

    SFLogDebug(@"待办推送成功");

}


-(void)sendRequestTodoRemindCycleFailwithItem:(ResponseItem *)responseItem

{

    [self hideWaitingHUD];

    [self showAutoDismissTextAlert:@"设置失败"];

    isSetFail = YES;

//若是设置失败,则恢复原来的设置

    NSIndexPath *selectedIndexPath = [self.customTableView indexPathForSelectedRow];

    [self tableView:self.customTableView didDeselectRowAtIndexPath:selectedIndexPath];

    NSArray *indexPathArray = [self.customTableView indexPathsForVisibleRows];

//若是是2选1的情况能够用下面加红部分的代码,若是是多选1能够用以前选择的那个cell执行 [self tableView:self.customTableView didSelectRowAtIndexPath:indexPath];这个方法

   for (NSIndexPath *indexPath in indexPathArray) {

        if (selectedIndexPath != indexPath) {

            [self tableView:self.customTableView didSelectRowAtIndexPath:indexPath];

        }

    }

    isSetFail = NO;

    SFLogDebug(@"待办推送失败-->%@",responseItem.errorInfo);

}


-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryNone;

    

}