【问题标题】:Change highlighting of certain subviews in a UITableViewCell更改 UITableViewCell 中某些子视图的突出显示
【发布时间】:2011-11-25 09:47:44
【问题描述】:

是否可以排除 UITableViewCell 中的某些子视图被突出显示或更改它们的突出显示实现?

我在某些情况下向 UITableViewCell 添加了一个橙色警告标签,它与单元格的其余部分一起正确突出显示,这很好。但是,当单元格停止以动画方式突出显示时,您首先会看到警告标签的背景变为白色,然后闪烁为橙色。 我希望它完全被排除在突出显示之外,或者更改它的突出显示实现,以便它正确地动画回橙色。

【问题讨论】:

  • 到目前为止,我已经发现覆盖 setHighlight:animated: 并立即返回会取消开始突出显示。但是当我按下单元格时,高亮仍然会将标签变为蓝色。
  • 其他地方也有类似的问题。我在这里发布了答案stackoverflow.com/a/36886209/81388

标签: ios uitableview


【解决方案1】:

如果超级视图需要,似乎无法将视图排除在突出显示之外。但是,如果您只是停止那里的任何突出显示行为或将其更改为您的规范,则覆盖他们的 setHighlighted: 可以得到相同的结果。

【讨论】:

    【解决方案2】:

    在 iOS 6 中,您可以使用 UITableViewDelegate 中的 2 种方法来自定义单元格和子视图的突出显示:

    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor lightGrayColor]];
    }
    
    - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [UIView animateWithDuration:0.5f animations:^{
            [cell setBackgroundColor:[UIColor whiteColor]];
        }];
    }
    

    我没有显示更改子视图的代码。

    但是您可以像为每个单元格配置的方式一样执行类似的操作,使用单元格viewWithTag 访问子视图,然后更改背景或任何其他属性。

    【讨论】:

      【解决方案3】:

      您不能从动画中删除某些子视图,但您可以更改一些行为,如下所示:

      避免 UITableViewCell 做这样的高亮动画:

      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      

      当一个单元格被选中时,像这个例子一样手动改变背景:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      
          [cell setBackgroundColor:[UIColor purpleColor]];
      
          [UIView animateWithDuration:0.5f animations:^{
                  [cell setBackgroundColor:[UIColor clearColor]];
          }];
      
      }
      

      我有一个简单的 UITableView,它可以满足我的需要。

      【讨论】:

        【解决方案4】:

        为了在表格视图单元格上具有自定义突出显示行为,重写 setHighlighted 方法并将 highlight:false 解析为 super 方法,并根据下面的突出显示属性自定义指定哪些视图应更改颜色

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-29
          • 2021-03-10
          • 1970-01-01
          • 1970-01-01
          • 2011-02-02
          • 2012-03-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多