簡體   English   中英

輕按時,Tableview單元格分隔線消失

[英]Tableview cell separating line disappears when tapped

我有一個UITableView帶有自定義tableview單元格,每個單元格之間都有分隔線。 我最近在編輯模式下開始實現多單元格選擇。 為了在選中每個單元格時都帶有藍色圓圈的選中標記,我將單元格selectionStyleUITableViewCellSelectionStyleNone更改為UITableViewCellSelectionStyleDefault 為了在選擇單元格時擺脫灰色陰影,我只是實現了白色背景,如下所示:

UIView * cellBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cellBackgroundView.backgroundColor = [UIColor clearColor];
cell.multipleSelectionBackgroundView = cellBackgroundView;
cell.selectedBackgroundView = cellBackgroundView;

問題是,當我不在編輯模式下時,只要選擇了單元格,分隔線就會消失,但是當我在編輯模式下選擇單元格時,分隔線仍然保留。 任何想法如何解決這個問題,以便分隔符始終存在?

嘗試以下操作:

添加這行

[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadRowsAtIndexPaths:@[indexPath]
                 withRowAnimation:UITableViewRowAnimationAutomatic];

在didSelectRowAtIndexPath的開頭

這是自iOS7.0起存在的奇怪錯誤

我知道單元格分隔符消失是一個常見問題,但是當前的解決方案似乎都無法解決我的問題,因此我得出的結論是,當tableView不在編輯模式下時,需要將selectionStyle切換為UITableViewCellSelectionStyleNone ,而在UITableViewCellSelectionStyleBlue時,我處於編輯模式。 我可以通過在包含表視圖的視圖控制器中調用以下命令來實現此目的:

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

    if(tableView.editing)
    {
        [((customTableViewCellClass*)[tableView cellForRowAtIndexPath:indexPath]) changeSelectionStyle:YES];
    }
    else
    {
        [((customTableViewCellClass*)[tableView cellForRowAtIndexPath:indexPath]) changeSelectionStyle:NO];
    }

return indexPath;
}

然后在我的自定義tableviewcell類中,調用以下命令:

-(void) changeSelectionStyle: (BOOL) selected
{
    if(selected)
    {
        self.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    else
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }

}

cellForRowAtIndexPath我留下了更改了multipleSelectionBackgroundView視圖的代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView * cellBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cellBackgroundView.backgroundColor = [UIColor clearColor];
    cell.multipleSelectionBackgroundView = cellBackgroundView;
    cell.selectedBackgroundView = cellBackgroundView;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM