UITableView separator line disappears when selecting cells in iOS7

后端 未结 24 2055
耶瑟儿~
耶瑟儿~ 2020-12-05 04:01

In my tableView I set a separator line between cells. I am allowing selection of multiple cells. Here\'s my code for setting selected cell background color:

         


        
相关标签:
24条回答
  • 2020-12-05 04:28

    For me it happened when I set programmatically:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    When i set this property in the storyboard it works fine.

    0 讨论(0)
  • 2020-12-05 04:30

    This problem exists for single cell selection as well.

    Another solution is to reload the table view, select followed by deselect:

    self.selectedIndex = inIndexPath.row;
    [inTableView reloadData];
    [inTableView selectRowAtIndexPath:inIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [inTableView deselectRowAtIndexPath:inIndexPath animated:YES];
    

    This gets rid of a subtle graphical selection glitch I saw in Mark's solution.

    0 讨论(0)
  • 2020-12-05 04:31

    I encountered this problem with IOS 13 and solved it in this way:

    1. In tableView cell storyboard or xib file choose Selection NONE

    2. In swift file of the cell override func:

      override func setHighlighted(_ highlighted: Bool, animated: Bool) {
      
          super.setHighlighted(highlighted, animated: animated)
      
          if highlighted {
              contentView.backgroundColor = .lightGray
          } else {
              contentView.backgroundColor = .clear
          }
      }
      

    I wanted to get effect of regular selection like in previous IOS versions but if you want to get something else then customize the function with your colors.

    0 讨论(0)
  • 2020-12-05 04:33

    Add this code at cell for row at indexpath

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-12-05 04:35

    This'll just happen if you let iOS apply its own default selected cell style. Best work around I found so far is to override the selected property implementation:

    in your cell subclass implementation:

        @synthesize selected = _selected;
    

    in the initialization method:

        // problem actually is caused when you set following 
        // to UITableViewCellSelectionStyleDefault, so:
    
        [self setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    

    overriding methods:

        - (BOOL)selected 
        {
            return _selected;
        }
        - (void)setSelected:(BOOL)selected animated:(BOOL)animated 
        {
            _selected = selected
            if (selected) {
    
                // apply your own selected style
    
            }
            else {
    
                // apply your own deselected style
    
            }
        }
    
    0 讨论(0)
  • 2020-12-05 04:36

    I encountered this issue when I set my cell's selection style to none programatically, and then when I SELECT my table cells programatically.

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell!
        if tableView == self.jobLevelTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! CheckboxCell
    
            // for testing purposes
            let checked = true
    
            // I used M13Checkbox here, in case anybody was wondering
            cell.checkbox.setCheckState(checked ? .checked : .unchecked, animated: false) 
    
            if checked {
                tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
            }
    
            // CULPRIT
            cell.selectionStyle = .none
            return cell
        }
    
        cell = UITableViewCell()
        return cell
    }
    

    When I set the selection style on the storyboard (and removing the code equivalent), the problem went away!

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