iPhone UITableView cells stay selected

前端 未结 7 1192
[愿得一人]
[愿得一人] 2020-12-28 11:50

in my UITableView sometimes cells stay selected after touching. Because it happens only occasionally, I\'m not able to reproduce the problem.

Any hints? Maybe it has

相关标签:
7条回答
  • 2020-12-28 12:35

    Use this method in your UITableViewCell class

    (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    // Just comment This line of code
    
    // [super setSelected:selected animated:animated];
    
    }
    

    or even you can try this code in didSelectRowAtIndexPath method

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     
      // Do some stuff when the row is selected

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    }

    0 讨论(0)
  • 2020-12-28 12:36

    If you are using a UITableViewController you can use:

    self.clearsSelectionOnViewWillAppear = YES;
    
    0 讨论(0)
  • 2020-12-28 12:42

    Just to expand on "Ed Marty"s answer, I chose to add the deselect in the "viewWillDisappear" method instead because I thought that it looked nicer.

    In addition to this I was using a UITableView with a regular UIViewController (rather than a UITableViewController) so the tableView variable wasn't available to me. To overcome this I added a tableView property in my view controllers header file (and in my XIB file I connected the actual table view to the property)...

    @property (nonatomic,retain) IBOutlet UITableView *tableView
    

    ...and in the view controllers implementation wired up the property and performed the deselect as follows..

    @synthesize tableView
    
    - (void) viewWillDisappear:(BOOL)animated {
        [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
        [super viewWillDisappear:animated];
    }
    
    0 讨论(0)
  • 2020-12-28 12:43

    In my case , i deselect the cell right at the end of didSelectRowAtIndexPath :

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // Do smth here. segue ..etc
    
        [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
    }
    
    0 讨论(0)
  • 2020-12-28 12:51

    I can't tell you why you're seeing the issue, but here are some suggestions for fixing it:

    According to the Apple HIG, the selection should not disappear until returning from the view controller just pushed onto the stack. If your controller is just a UITableViewController, it should deselect automatically upon returning to the view. If not, add

    - (void) viewWillAppear:(BOOL)animated {
        [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated];
        [super viewWillAppear:animated];
    }
    

    somewhere in the view controller.

    If there are any rows that, when clicked, do not go to another view, and don't in fact do anything when selected, they shouldn't be selectable, so you can override that in

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    and return nil in the cases that the row should not be selected.

    0 讨论(0)
  • 2020-12-28 12:54

    Solving this takes about two seconds once you learn how tables are managed.

    As soon as cells are scrolled off screen, they lose their formatting. The table minimizes its memory usage by dismissing the other cells and recreating them if/when the user scroll back.

    Even things like reloading table data, that really only applies to the cells on screen at that moment. Point being, the cellForRowAtIndexPath might create the same cell five times without the user ever hitting a button or leaving the view controller! So set it up to reload any formatting. Add something like this to cellForRowAtIndexPath:

        if arrayOf_SelectedCells.contains(indexPath) {
            setup_Selected(cell)
            //Where setup_Selected is a custom function to apply formatt
        } else {
            setup_Unselected(cell)
        }
    
    0 讨论(0)
提交回复
热议问题