【问题标题】:Deselecting the table view row when returning返回时取消选择表格视图行
【发布时间】:2010-10-20 11:47:09
【问题描述】:

我在这里按照 Luke Redpath 的建议 - Selected UItableViewCell staying blue when selected - 在返回原始表格视图时取消选择该行,但我无法让它工作。事实上,该方法并没有被触发。

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

我怀疑这是因为该类不是UITableViewController,而是UIViewController,其中tableView 作为连接到NIB 的属性。

如何获得相同的行为 - 即返回时取消选择?

【问题讨论】:

  • 只要 tableView 正确连接就没有关系。你应该可以把它放在didSelectRowAtIndexPath: 中并让它工作。您可能还想对 [self.tableView indexPathForSelectedRow] 返回的内容进行快速 NSLog 检查,因为它实际上可能返回 nil。
  • 我使用了这个,但遇到了一个问题,即单元格中的更改(背景等)没有立即反映。在 didDeselectRowAtIndexPath: 中添加 reloadRowsAtIndexPaths: 成功了

标签: iphone objective-c uitableview ios


【解决方案1】:

为什么你不在委托方法中取消选择它

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

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

}

【讨论】:

  • 你不用[self.tableView indexPathForSelectedRow],indexPath已经是个参数了,直接插入就行了。
  • 另外,如果您使用委托方法,您应该使用tableView 参数而不是self.tableView,因为如果您有多个表视图,它们可能会有所不同。
【解决方案2】:

如果您使用的是 NavigationController,则可以使用它的委托:

- (void)navigationController: (UINavigationController *)navigationController
       didShowViewController: (UIViewController *)viewController
                    animated: (BOOL)animated
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

但你必须检查显示控制器是否是你想要的 =/

[编辑] 创建插座并将其在 IB 中链接到 NavigationController

@interface MyInt <UINavigationControllerDelegate>
{
    IBOutlet UINavigationController *navigation;
    ...
}
@property (nonatomic, retain) UINavigationController *navigation;
...
@end

@implementation MyInt
@syntesize navigation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigation setDelegate: self];
}
...

@end

【讨论】:

  • 我的导航控制器是在我的笔尖中创建的,所以它没有对应的类。有办法解决吗?
  • 您可以将其映射到 IBOutlet ivar。
  • 我的设置是我有一个带有相应 NIB 的 MainViewController 类。该笔尖具有 UITabBarController(也是 MainViewController 中的 IBOutlet 属性“tabBarController”)。该选项卡栏中有几个 UINavigation 控制器。这些没有在代码中引用。所以我不确定如何访问这些导航控制器
【解决方案3】:

您是否以编程方式选择行?如果是这样,我在使用[cellToBeSelected setSelected:YES]; 选择行时遇到了同样的问题。

通过使用UITableViewControllerselectRowAtIndexPath:animated:scrollPosition: 方法,它跟踪当前选定的indexPath。

实际上,如果clearsSelectionOnViewWillAppear 设置为true,则无需手动取消选择行。

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,但调试后我发现 indexPathForSelectedRow 计数为 0,所以我仔细检查了我的代码,发现我在调用

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // some code...
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    删除此行后,它按预期工作。

    【讨论】:

    • 这是一个非常明显的错误让我着迷。我认为包括我自己在内的很多人出于习惯将取消选择委托给了。
    【解决方案5】:

    非常简单:试试这条线。

    在您的didSelectRowAtIndexPath 中添加deselectRowAtIndexPath

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    }
    

    【讨论】:

      【解决方案6】:

      Swift 3.0

      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                  tableView.deselectRow(at: indexPath, animated: true)
          //Write Your Other code Here.
                  }
      

      点击后会取消选择行。

      【讨论】:

        【解决方案7】:
        -(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
        {
            return YES;
        }
        
        -(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
        {
            [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor grayColor]];
        }
        
        -(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
        {
            [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor clearColor]];
        }
        

        【讨论】:

        • 请描述为什么这是解决方案。有什么变化/技巧?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多