Deselecting the table view row when returning

I followed Luke Redpath's suggestion here - Selected UItableViewCell staying blue when selected - to deselect the row when returning to the original table view, but I can't get it working. In fact the method doesn't get triggered.

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

I suspect this is because the class isn't a UITableViewController , but a UIViewController with the tableView as a property connected up to the NIB.

How would I get the same behavior - ie deselecting when returning?


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

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

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

}

If you are using a NavigationController, you can do it with its delegate:

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

But you have to check whether the show controller is which you want =/

[EDIT] Create the outlet and link it in IB to 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

Are you selecting the row programmatically? If so, I had the same problem when selecting a row using [cellToBeSelected setSelected:YES]; .

By using the UITableViewController 's selectRowAtIndexPath:animated:scrollPosition: method it kept track of the current selected indexPath.

In fact, if clearsSelectionOnViewWillAppear is set to true, one does not have deselect rows manually.

链接地址: http://www.djcxy.com/p/60012.html

上一篇: 重新载入表格视图数据并取消选择单元格

下一篇: 返回时取消选择表格视图行