在iOS7中选择单元格时,UITableView分隔线消失

art*_*ras 49 uitableview ios7

在我的tableView中,我在单元格之间设置了一个分隔线.我允许选择多个细胞.这是我设置所选单元格背景颜色的代码:

UIView *cellBackgroundColorView = [[UIView alloc] initWithFrame:cell.frame];
[cellBackgroundColorView setBackgroundColor:[UIColor darkGray]];
[cell setSelectedBackgroundView:cellBackgroundColorView];
Run Code Online (Sandbox Code Playgroud)

问题是如果选择了两个相邻的单元格,iOS7中它们之间没有分隔线,而iOS6中有(如预期的那样).

我甚至尝试将cellBackgroundColorView帧高设置为cell.frame - 1.0,但这也不起作用.

有任何想法吗?

Mar*_*ark 37

我还没有深究它(乍一看它似乎是一个iOS 7的bug ..),但我找到了一个解决方法.在tableView:didSelectRowAtIndexPath中,如果您发送以下两条消息,则可以直观地解决问题(可能的性能成本).

[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
Run Code Online (Sandbox Code Playgroud)

为了这个(对我而言),deselectRowAtIndexPath:animated:必须包含动画:YES.用于reloadRowsAtIndexPaths的动画:withRowAnimation:无关紧要.

  • @Mark,如果我尝试使用你的代码,它会导致我选择的单元格最终未被选中,这就失败了.我希望选择多个(相邻)单元格(突出显示背景)并且分隔符可见. (2认同)

小智 24

在单元格中为indexpath处的行添加此代码

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

  • 我糊涂了!`cell.selectionStyle = UITableViewCellSelectionStyleNone;`使得选择单元格变得不可能!这怎么能解决手头的问题? (13认同)

GOr*_*o58 16

在我的情况下,我是动画一行,所以我需要把这样的东西:

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

[tableView beginUpdates];
[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
//if you are doing any animation you have deselect the row here inside. 
[tableView endUpdates];
} 
Run Code Online (Sandbox Code Playgroud)


Jam*_*far 12

@ samvermette的答案为我解决了这个问题,但我不得不首先取消选择所选的行.

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

    //Deselect Row
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    // fix for separators bug in iOS 7
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; }
Run Code Online (Sandbox Code Playgroud)

  • 如果您的表启用了多个选择选项(即,当您需要使用复选标记附件视图时),该行为与您的解决方案冲突.问题是选择时调用的deselectRowAtIndexPath:animated:方法.还有另外一种方法吗? (2认同)

小智 6

在你的 UITableViewCell 类中传递它。

override func layoutSubviews() {
    super.layoutSubviews()

    subviews.forEach { (view) in
        if type(of: view).description() == "_UITableViewCellSeparatorView" {
            view.alpha = 1.0
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

当我以编程方式将单元格的选择样式设置为none时,然后以编程方式选择表单元格时,遇到了此问题。

    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
}
Run Code Online (Sandbox Code Playgroud)

当我在情节提要板上设置选择样式(并删除等效的代码)时,问题就消失了!

故事板很有用