UITableView 单元格分隔符在选中时消失

Posted

技术标签:

【中文标题】UITableView 单元格分隔符在选中时消失【英文标题】:UITableView cell's seperators disappear when selected 【发布时间】:2015-05-15 11:32:35 【问题描述】:

当我选择 UITableViewCell 并开始滚动到单元格从屏幕上消失的位置时,这些单元格上的分隔符会在它们重新出现在屏幕上时消失。

代表:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if(cell == nil)
        if(Type == Scene)
            Group *scene = [[appdata getScenes]objectAtIndex:lastRequestedPropertyPosition];
            [selectedArray addObject:scene.id];
        else if(Type == Product)
            Device *device = [[appdata getDevices]objectAtIndex:lastRequestedPropertyPosition];
            [selectedArray addObject:device.id];
        
    else
        [selectedArray addObject:[NSNumber numberWithInt:cell.tag]];
    

数据来源:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"protoCell" forIndexPath:indexPath];
    if (!cell) 
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"protoCell"];
    
    Group *group = [[appdata getScenes] objectAtIndex:indexPath.row];
    if(group != nil)
        [cell.textLabel setText:group.name];
    

    cell.tag = group.id.intValue;
    return cell;

谁能告诉这里出了什么问题?

谢谢

编辑 使用它只会使分隔符在选择时消失,而不是仅仅将分隔符保留在那里。

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if(cell == nil)
        if(Type == Scene)
            Group *scene = [[appdata getScenes]objectAtIndex:lastRequestedPropertyPosition];
            [selectedArray addObject:scene.id];
        else if(Type == Product)
            Device *device = [[appdata getDevices]objectAtIndex:lastRequestedPropertyPosition];
            [selectedArray addObject:device.id];
        
    else
        [selectedArray addObject:[NSNumber numberWithInt:cell.tag]];
    

【问题讨论】:

这个链接可以帮助你***.com/questions/19212476/… 【参考方案1】:

我的问题的原因是我使用蓝色阴影来显示被选中的单元格。然而,这种阴影有点太大了。它覆盖了分隔符

【讨论】:

【参考方案2】:

tableView:didSelectRowAtIndexPath 中,如果您同时发送下面的两条消息,问题就会在视觉上得到解决(可能会降低性能成本)。

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

为了使这个工作(对我来说),deselectRowAtIndexPath:animated: 必须包含animated:YES。用于reloadRowsAtIndexPaths:withRowAnimation: 的动画无关紧要。

【讨论】:

以上是关于UITableView 单元格分隔符在选中时消失的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 的单元格分隔符在滚动后消失

iOS UITableView 移除单元格选中时的高亮状态

点击时表格视图单元格分隔线消失

UITableViewCell 和消失的分隔符

选择单元格时,图像视图消失但 UILabel 消失

UITableViewCell 子视图显示在单元格之外