【问题标题】:UIView disappears when UITableViewCell is tapped点击 UITableViewCell 时 UIView 消失
【发布时间】:2017-01-03 14:10:18
【问题描述】:

在我的 iOS 应用程序中,我目前正在努力解决我在情节提要中设计的自定义 UITableViewCell 的问题。

当我点击使用此特定 UITableViewCell 呈现的表格视图行时会出现此问题。

这是使用此 UITableViewCell 呈现的行的默认外观:

这是我点击后的样子:

如您所见,绿线消失了,并且在点击该行之前一直是不可见的。一旦该行被释放,它就会恢复默认外观,一切都很好。

这是我期待看到的:

这是故事板中显示的 UITableViewCell 结构。 RouteView 是绿线 UIView 项:

我还为 UIView 对象添加了情节提要的属性部分:

我有一个附加到 UITableViewCell 的类,它通过 IBOutlets 处理对象的布局,而对行的点击是在 UITableView 级别处理的。

我根据某些条件更改 UIView 的背景颜色,但它可以是绿色、红色和蓝色,从不透明。是不是和this 有关系?

我该如何解决这个问题? 提前谢谢你。

【问题讨论】:

  • 您是否将 routeView 设置为隐藏在任何地方?
  • @Rikh 不。处理 UITableViewCell 上的点击事件的函数被调用但现在但它是空的,没有执行任何逻辑。我猜问题应该在布局级别的某个地方

标签: ios uitableview uiview


【解决方案1】:

我已经设法找到了this answer 中描述的解决方案,但在我的情况下是 obj-c。

显然,当 UITableViewCell 被选中或突出显示时会自动删除所有封闭的视图背景颜色(我认为是 [UIColor clearColor])。

我正在做的是在对 setSelected 或 setHighlighted 进行超级方法调用之前获取视图的颜色,然后立即将颜色设置为背景。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    // Gets route view background color before doing setSelected animation
    UIColor *routeViewBkgColor = self.routeView.backgroundColor;
    [super setSelected:selected animated:animated];

    // Set route view background color after setSelected animation
    self.routeView.backgroundColor = routeViewBkgColor;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    // Gets route view background color before doing setSelected animation
    UIColor *routeViewBkgColor = self.routeView.backgroundColor;
    [super setHighlighted:highlighted animated:animated];

    // Set route view background color after setSelected animation
    self.routeView.backgroundColor = routeViewBkgColor;
}

通过这种方式,我能够 100% 获得保持选择行为的预期效果,而不会丢失所有视图的背景颜色。

【讨论】:

    【解决方案2】:

    当前的两个答案都是正确的,但以防万一您使用的是 Objective-C 而不是 swift。

    你必须使用cell.selectionStyle = UITableViewCellSelectionStyleNone;

    或者,如果你想要“点击”的效果,那么你必须使用

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    }
    

    希望对您有所帮助! :-)

    【讨论】:

      【解决方案3】:

      您可能需要将单元格的选择样式设置为无。您可以像 Nirav D 提到的那样以编程方式执行此操作,也可以通过您的故事板执行此操作(参见图片)。

      【讨论】:

      • 就像我说的 @Nirav 回答将 selectionStyle 设置为 none 只会从行中删除选择效果。相反,我想保留它,但不会丢失问题中解释的绿线。
      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多