UITableView does not automatically deselect the selected row when the table re-appears

前端 未结 7 587
感动是毒
感动是毒 2021-02-03 17:45

Normally a selected row in a UITableView gets deselected with an animation when the user pops back from the detail view.

However, in my case where I have a

相关标签:
7条回答
  • 2021-02-03 18:10

    When your main ViewController is from type UITableViewController, it has a property clearsSelectionOnViewWillAppear, which is per default YES - so it will clear the selection automatically.

    This property is not available for an UITableView, i guess it's because it has no ViewWillAppear method either.

    A UIViewController doesn't need this property because it has no UITableView originally.

    conclusion: you'll have to implement it by yourself when you do not use a UITableViewController.

    0 讨论(0)
  • 2021-02-03 18:10

    Do the deselection in didSelectRowAtIndexPath instead of viewWillAppear:

    - (void)tableView:(UITableView *)tableView
                      didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
         //show the second view..
         [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
     }
    
    0 讨论(0)
  • 2021-02-03 18:12

    In Swift 3 / 4

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    0 讨论(0)
  • 2021-02-03 18:19

    Nothing is wrong--deselecting the highlighted row is always "manual". If you look at Apple's sample code, you will see the same thing.

    0 讨论(0)
  • 2021-02-03 18:21

    I dont think deselecting the selected row is automatic... I normally do it before pushing to the next view

    - (void)tableView:(UITableView *)tableView 
            didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        // to do other things
        [self.navigationController pushViewController:yourNextViewController animated:YES];
    }
    
    0 讨论(0)
  • 2021-02-03 18:25

    In swift, you can add the following lines in your viewWillAppear

    if let row = tableView.indexPathForSelectedRow() {
        tableView.deselectRowAtIndexPath(row, animated: true)
    }
    

    In swift 2, it's without parantheses:

    if let row = tableView.indexPathForSelectedRow {
        tableView.deselectRowAtIndexPath(row, animated: true)
    }
    

    In Swift 4 (and 3?) the function name was cleaned up:

    if let indexPath = tableView.indexPathForSelectedRow {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    0 讨论(0)
提交回复
热议问题