Why does UITableViewCell remain highlighted?

前端 未结 18 1259
余生分开走
余生分开走 2020-12-12 12:45

What would cause a table view cell to remain highlighted after being touched? I click the cell and can see it stays highlighted as a detail view is pushed. Once the detail

相关标签:
18条回答
  • 2020-12-12 13:03

    For Swift 3: I would prefer it to use in viewDidDisappear

    Define:-

        var selectedIndexPath = IndexPath()
    

    In viewDidDisappear:-

        override func viewDidDisappear(_ animated: Bool) {
        yourTableView.deselectRow(at: selectedIndexPath, animated: true)
    }
    

    In didSelectRowAtIndexPath:-

        func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
        selectedIndexPath = indexPath
    }
    
    0 讨论(0)
  • 2020-12-12 13:05

    Swift 3 Solution

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    }
    
    0 讨论(0)
  • 2020-12-12 13:09

    To get the behaviour Kendall Helmstetter Gelner describes in his comment, you likely don't want deselectRowAtIndexPath but rather the clearsSelectionOnViewWillAppear property on your controller. Perhaps this was set to YES by accident?

    See the comment in the default Apple template for new UITableViewController subclasses:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;
    }
    
    0 讨论(0)
  • 2020-12-12 13:09

    Use this method in UITableViewCell class

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
       // Just comment This line of code
       // [super setSelected:selected animated:animated];        
    }
    
    0 讨论(0)
  • 2020-12-12 13:10

    if the cell is remaining highlighted after touching it, you can call UITabelView method,

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

    `[tableView deselectRowAtIndexPath:indexPath animated:YES];`   
    

    }

    Or, you can use the following method and modify it according to your requirements,

    // MARK: UITableViewDelegate

    func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
      if let cell = tableView.cellForRowAtIndexPath(indexPath) {
         cell.backgroundColor = UIColor.greenColor()
      }
    }
    
    func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
      if let cell = tableView.cellForRowAtIndexPath(indexPath) {
         cell.backgroundColor = UIColor.blackColor()
      }
    } 
    
    0 讨论(0)
  • 2020-12-12 13:11

    If you are using a UITableViewCell, then comment the following line

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
    
     // [super setSelected:selected animated:animated];
    
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题