用UITableView实现单选(打对勾)

本文介绍了一个iOS应用中用于设置提醒频率的TableView实现方案。通过自定义TableView及其单元格,实现了不同提醒周期选项间的切换与选择确认功能。文章详细展示了如何为TableView配置数据源及代理方法,包括单元格样式调整、选中状态反馈以及与服务器交互完成设置的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


如果有更好的方法,希望各位不吝赐教

@interface SetRemindCycleViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    NSIndexPath *current;

    NSInteger recordRow;

    BOOL isSetFail;

}

//自定义TableView

@property (nonatomic, strong) UITableView *customTableView;

@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;

    

}


转载于:https://my.oschina.net/daxiaLKS/blog/537868

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值