zoukankan      html  css  js  c++  java
  • iOS UI 的坑:不要 remove UITableViewCell 的 contentView

    看到标题,你可能会想,怎会有人傻到这么做?好吧,一个像我一样没有经验的程序员的确可能。 这个问题的背景是,在需要重绘UITableViewCell时,经常遇到需要清空所有subview的情况。而我们有这样一段代码可以利用:

    UIView+Utils.m

    - (void)removeAllSubviews {

       for (UIView *subView in self.subviews) {

       [subView removeFromSuperview];

      }

    }

    正确的做法

      正确的做法是,把所有的子 view 都加在 contentView上。

    SomeTableViewCell.m

      [self.contentView removeAllSubviews];

      // ……

      [self.contentView addSubview:view];

    错误的做法

      错误的做法是,把子 view 加在 cell 本身的 view 上。

    SomeTableViewCell.m

      [self removeAllSubviews];

      // ……

      [self addSubview:view];

    这样在removeAllSubviews时,不仅 remove 掉了自己添加的那些子 view,也一同 remove 掉了UITableViewCellcontentView

    错误的后果

    错误的做法造成的后果是:在 iOS 7(测试用版本为7.1)上,cell 显示为一片空白。所有的 subview 都显示不出来。在 iOS 6 和 iOS 8 上均正常。

  • 相关阅读:
    172. Factorial Trailing Zeroes
    96. Unique Binary Search Trees
    95. Unique Binary Search Trees II
    91. Decode Ways
    LeetCode 328 奇偶链表
    LeetCode 72 编辑距离
    LeetCode 226 翻转二叉树
    LeetCode 79单词搜索
    LeetCode 198 打家劫舍
    LeetCode 504 七进制数
  • 原文地址:https://www.cnblogs.com/ChouDanDan/p/5112645.html
Copyright © 2011-2022 走看看