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

    

}

相關文章
相關標籤/搜索