关于deselectRowAtIndexPath

本文探讨了UITableView在导航控制器中push和pop视图时,如何处理cell选中状态的问题。详细介绍了不同场景下(UITableViewController与UIViewController)的默认行为及解决方案,并提供了一种更理想的实现方法。
有没有遇到过,导航+UITableView,在push,back回来之后,当前cell仍然是选中的状态。
当然,解决办法简单,添加一句[tableView deselectRowAtIndexPath:indexPath animated:YES]即可。
令人纠结的时,在没加这句的时候,有的视图同样回来之后,选中状态消失,为什么会出现这种情况呢?
原来是,如果UITableView是在UITableViewController中时,就会默认取消,而如果是在UIViewController时,需要添加这一句,不过有时即使前者也需要添加,那是因为在视图加载时有其它功能代码,具体情况各异。所以后者必须加,前者可能需要加。

当然如果要求高的话,另外一种更加理想的办法是:
- (void) viewWillAppear: (BOOL)inAnimated {
NSIndexPath *selected = [self.table indexpathForSelectedRow];
if(selected) [self.table deselectRowAtIndexpath:selected animated:NO];
}

这种方法是在放回的过程中逐渐取消选中状态的,可以提示刚才点进去的是哪一行,默认的也正是这种效果。
分析下面代码的逻辑,但这个页面完成最后设置时,需要解决下面的bug bug信息: Bug 1174015 - 【quick setup】iOS standalone添加ap,quick setup最后一步summary页控件依然显示为Next,安卓为“Confirm”引导更合理 (edit) 基本信息 ► 审核区域▼ Status: ASSIGNED (edit) Importance: major (edit) Bug的类别: UI (edit) 出现频率: 必然 (edit) 能否恢复: --- (edit) 测试拓扑: (edit) 陪测设备: (edit) 重现步骤: iOS standalone添加ap,quick setup到最后一步summary页,观察 (edit) 测试结果: 控件依然显示为Next (edit) 期望结果: 显示为confirm (edit) 问题产品: (edit) 对比测试: Android显示为confirm (edit) 问题分析: 请分析 (edit) 影响评估: 双端不一致 (edit) 代码: // // StandaloneQuickSetupSummaryViewController.m // Omada // // Created by zhuangqiuxiong on 2018/1/8. // Copyright © 2018年 TP-Link. All rights reserved. // #import "StandaloneQuickSetupSummaryViewController.h" #import "VMSLQuickSetupSummary.h" #import "StandaloneQuickSetupApplySettingsViewController.h" #import "TPSLQuickSetupUserAbstractLayer.h" #import "standaloneQuickSetupFlexibleBar.h" @interface StandaloneQuickSetupSummaryViewController ()<UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) VMSLQuickSetupSummary *vmTableViewDataSource; @property (nonatomic, strong) StandaloneQuickSetupFlexibleBar *flexibleBar; @property (nonatomic, strong) UIButton *nextButton; @property (nonatomic, assign) BOOL isForGateway; @end @implementation StandaloneQuickSetupSummaryViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setupNavigationItem]; [self buildTableView]; [self buildFlexibleBar]; } - (void)buildTableView { self.tableView = [TPBCommonTableHelper createKeyboardAvoidingTableViewWithDelegate:self andDataSource:nil]; // self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; [self.view addSubview:self.tableView]; // self.tableView.delegate = self; // self.tableView.estimatedRowHeight = 72; // self.tableView.estimatedSectionHeaderHeight = 0; // self.tableView.estimatedSectionFooterHeight = 0; // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // self.tableView.separatorInfo.separatorStyle = TPTableViewSeparatorDetailStyle; // self.tableView.tableFooterView = [[UIView alloc] init]; // self.tableView.backgroundColor = [UIColor tpbBackground]; self.vmTableViewDataSource = [[VMSLQuickSetupSummary alloc] init]; self.tableView.dataSource = self.vmTableViewDataSource; [self.vmTableViewDataSource registerClassInTableView:self.tableView]; UIButton *nextButton = [[UIButton alloc] init]; if (nextButton) { [self.view addSubview:nextButton]; [nextButton setBackgroundColor:[UIColor tpbPrimary]]; nextButton.layer.cornerRadius = 22; [nextButton setTitle:gStandaloneQuickSetup.standaloneQuickSetupSetAccountNavRightButton forState:UIControlStateNormal]; nextButton.titleLabel.font = [UIFont tpr20Regular]; nextButton.titleLabel.textColor = [UIColor tpbTextWhite]; [nextButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; TPWeakSelf; [nextButton mas_makeConstraints:^(MASConstraintMaker *make) { TPStrongSelf; make.leading.mas_equalTo(_self.view).offset(20); make.trailing.mas_equalTo(_self.view).offset(-20); make.height.equalTo(@(44)); make.bottom.equalTo(self.mas_tpSafeAreaLayoutGuide).offset(-8); }]; self.nextButton = nextButton; } TPWeakSelf; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { TPStrongSelf; make.top.leading.trailing.equalTo(self.mas_tpSafeAreaLayoutGuide); make.bottom.equalTo(self.nextButton.mas_top).offset(-2); }]; } - (void)buildFlexibleBar { CGFloat maxBarHeight = kFlexibleBar_MinHeight + 50.0f; CGFloat minBarHeight = kFlexibleBar_MinHeight; self.flexibleBar = [[StandaloneQuickSetupFlexibleBar alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, maxBarHeight) andMaximumBarHeight:maxBarHeight andMinimumBarHeight:minBarHeight andElasticMaximumHeightAtTop:NO andViewController:self andScrollView:self.tableView]; if ( self.flexibleBar ) { [self.view addSubview:self.flexibleBar]; self.flexibleBar.titleString = gStandaloneQuickSetup.standaloneQuickSetupSummaryNavTitle; [self.flexibleBar setupSubViewsAndLayoutAttributes]; // self.flexibleBar.backgroundColor = [UIColor tpbBackground]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tpbPrivacyProtectionEnabled = YES; [self.vmTableViewDataSource reloadData]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - TableView Delegate - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { // return 40; return UITableViewAutomaticDimension; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [self.vmTableViewDataSource viewForSectionHeaderInSection:section]; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { // return 0.01; return TPBDesign.list.sectionFooterHeight; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return [UIView new]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark - 导航栏相关 - (void)setupNavigationItem { // [self setNavigationBarTitle:gStandaloneQuickSetup.standaloneQuickSetupSummaryNavTitle]; // [self.tpNavigationController setNavigationBarTitleFont:[UIFont tpr20Regular] andColor:[UIColor tpbTextPrimary]]; self.isNavigationBarTransparent = YES; UIBarButtonItem *leftBarButtonItem = [self createBarButtonItemItemWithStyle:TPViewControllerBarButtonItemStyleBackInBlue andTarget:self andAction:@selector(back)]; self.navigationItem.leftBarButtonItem = leftBarButtonItem; } - (void)back { [self.tpNavigationController popViewControllerAnimated:YES]; } - (void)next { TPWeakSelf; [self showWaitingToastView]; [[[ALStandaloneDeviceContext currentInstance] checkLoginStatus] addCompletionOnMainThread:^(TPHandleResult *result) { TPStrongSelf; [_self dismissToastViewWithCompleteBlock:^{ if (result.success) { if (self.isForGateway) { // 跳转到Gateway页面 [_self.tpNavigationController pushViewControllerWithCustomAnimation:[[StandaloneQuickSetupApplySettingsViewController alloc] initForGateway:self.isForGateway] animated:YES]; } else { // 跳转到EAP页面 [_self.tpNavigationController pushViewControllerWithCustomAnimation:[[StandaloneQuickSetupApplySettingsViewController alloc] init] animated:YES]; } } else { DDLogInfo(@"检查QuickSetup前置条件不满足"); [self showCommonFailedToastView]; } }]; }]; } - (void)orientationChangeAction { //横屏模式下 if (self.orientation == TPBInterfaceOrientationRight || self.orientation == TPBInterfaceOrientationLeft) { CGFloat maxBarHeight = kFlexibleBar_MinHeight + 50.0f; self.flexibleBar.frame = CGRectMake(0, 0, MAX(kScreen_Width, kScreen_Height), maxBarHeight); } //竖屏模式下 else { CGFloat maxBarHeight = kFlexibleBar_MinHeight + 50.0f; self.flexibleBar.frame = CGRectMake(0, 0, MIN(kScreen_Width, kScreen_Height), maxBarHeight); } } // MARK: 工具函数 - (BOOL)isForGateway { return [TPStandaloneClient currentInstance].discoveredDevice.isGateway; } @end
最新发布
12-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值