How to deselect a selected UITableView cell?

前端 未结 24 3122
一整个雨季
一整个雨季 2020-12-07 10:10

I am working on a project on which I have to preselect a particular cell.

I can preselect a cell using -willDisplayCell, but I can\'t deselect it when t

相关标签:
24条回答
  • 2020-12-07 10:30

    Can you try this?

    - (void)tableView:(UITableView *)tableView 
      didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        AppDelegate_iPad *appDelegte = 
        (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
        NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
        appDelegte.indexPathDelegate = indexPath;
    
        UITableViewCell *prevSelectedCell = [tableView cellForRowAtIndexPath: indexpath1];
        if([prevSelectedCell isSelected]){
           [prevSelectedCell setSelected:NO];
        }
    }
    

    It worked for me.

    0 讨论(0)
  • 2020-12-07 10:33

    Based on saikirans solution, I have written this, which helped me. On the .m file:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {
    
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            selectedRowIndex = nil;
    
        }
    
        else {  self.selectedRowIndex = [indexPath retain];   }
    
        [tableView beginUpdates];
        [tableView endUpdates];
    
    }
    

    And on the header file:

    @property (retain, nonatomic) NSIndexPath* selectedRowIndex;
    

    I am not very experienced either, so double check for memory leaks etc.

    0 讨论(0)
  • 2020-12-07 10:34

    use this code

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
     {
        //Change the selected background view of the cell.
         [tableView deselectRowAtIndexPath:indexPath animated:YES];
     }
    

    Swift 3.0:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //Change the selected background view of the cell.
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    0 讨论(0)
  • 2020-12-07 10:34

    Use the following method in the table view delegate's didSelectRowAtIndexpath (Or from anywhere)

    [myTable deselectRowAtIndexPath:[myTable indexPathForSelectedRow] animated:YES];
    
    0 讨论(0)
  • 2020-12-07 10:35

    To have deselect (DidDeselectRowAt) fire when clicked the first time because of preloaded data; you need inform the tableView that the row is already selected to begin with, so that an initial click then deselects the row:

    //Swift 3:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        if tableView[indexPath.row] == "data value"
    
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
    
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:36

    It might be useful to make an extension in Swift for this.

    Swift 4 and Swift 5:

    Swift extension (e.g. in a UITableViewExtension.swift file):

    import UIKit
    
    extension UITableView {
    
        func deselectSelectedRow(animated: Bool)
        {
            if let indexPathForSelectedRow = self.indexPathForSelectedRow {
                self.deselectRow(at: indexPathForSelectedRow, animated: animated)
            }
        }
    
    }
    

    Use e.g.:

    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
    
        self.tableView.deselectSelectedRow(animated: true)
    }
    
    0 讨论(0)
提交回复
热议问题