UITableViewCell doesn't get deselected when swiping back quickly

后端 未结 16 1368
甜味超标
甜味超标 2021-01-29 23:49

I\'ve now updated three of my apps to iOS 7, but in all three, despite them not sharing any code, I have the problem where if the user swipes to go back in the navigation contro

相关标签:
16条回答
  • 2021-01-30 00:40

    Use

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    code in

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method

    0 讨论(0)
  • 2021-01-30 00:43

    For swift

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    
        guard let indexPath = tableView.indexPathForSelectedRow else{
            return
        }
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    0 讨论(0)
  • 2021-01-30 00:45

    I'm dealing with the same problem right now. The UICatalog-sample from Apple seems to bring the dirty solution.

    It really doesn't make me happy at all. As mentioned before it uses [self.tableView deselectRowAtIndexPath:tableSelection animated:NO]; to deselect the currently selected row.

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        // this UIViewController is about to re-appear, make sure we remove the current selection in our table view
        NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
        [self.tableView deselectRowAtIndexPath:tableSelection animated:NO];
    
        // some over view controller could have changed our nav bar tint color, so reset it here
        self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
    }
    

    I have to mention the sample code may not be iOS 7 iOS 8 iOS 9 iOS 10-ready


    Something which really confuses me is the UITableViewController Class Reference:

    When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

    This is exactly the behavior I expect… but it does not seem to work. Neither for you nor for me. We really have to use the "dirty" solution and do it on our own.

    0 讨论(0)
  • 2021-01-30 00:46

    Based on Rhult's code, I made a few changes.

    This implementation allow user to cancel swipe back and still keep selected for future swipe back deselect animation

    @property(strong, nonatomic) NSIndexPath *savedSelectedIndexPath;
    
    
    - (void)viewDidLoad {
       [super viewDidLoad];
       self.clearsSelectionOnViewWillAppear = NO;
    }
    
    -(void) viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
       self.savedSelectedIndexPath = nil;
    }
    
    -(void) viewWillDisappear:(BOOL)animated {
       [super viewWillDisappear:animated];
       if (self.savedSelectedIndexPath && ![self.tableView indexPathForSelectedRow]) {
           [self.tableView selectRowAtIndexPath:self.savedSelectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
       } else {
          self.savedSelectedIndexPath = [self.tableView indexPathForSelectedRow];
       }
    }
    
    0 讨论(0)
  • 2021-01-30 00:50

    You are probably not calling the super view's viewWillAppear method ([super viewWillAppear:animated];). When you do this and the UITableViewController's parameter clearsSelectionOnViewWillAppear is YES then the cells will be deselected on viewWillAppear.

    0 讨论(0)
  • 2021-01-30 00:50

    Rhult's solution works perfectly on iOS 9.2. This is the implementation in Swift:

    Declare a variable in your MasterViewController to save the IndexPath:

    var savedSelectedIndexPath: NSIndexPath?
    

    Then you can put the code in an extension for clarity:

    extension MasterViewController {
        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)
            self.savedSelectedIndexPath = nil
        }
    
        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)
            if let indexPath = self.savedSelectedIndexPath {
                self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
            }
        }
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            self.savedSelectedIndexPath = tableView.indexPathForSelectedRow
            if let indexPath = self.savedSelectedIndexPath {
                self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题