返回时取消选择表视图行

时间:2010-10-20 11:47:10

标签: iphone objective-c uitableview ios

我遵循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作为属性连接到NIB的tableView

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

7 个答案:

答案 0 :(得分:21)

为什么你不在委托方法

中取消选择它
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

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

}

答案 1 :(得分:16)

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

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

但你必须检查show controller是否是你想要的= /

[编辑] 创建插座并将其链接到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

答案 2 :(得分:8)

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

通过使用UITableViewController的{​​{1}}方法,它可以跟踪当前选定的indexPath。

实际上,如果selectRowAtIndexPath:animated:scrollPosition:设置为true,则表示没有手动取消选择行。

答案 3 :(得分:8)

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

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

    // some code...
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

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

答案 4 :(得分:4)

很简单:试试这一行。

deselectRowAtIndexPath

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

}

答案 5 :(得分:2)

Swift 3.0

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

点击它后会取消选择Row。

答案 6 :(得分:0)

-(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]];
}
相关问题