看到標題,你可能會想,怎會有人傻到這麼作?好吧,一個像我同樣沒有經驗的程序員的確可能。
這個問題的背景是,在須要重繪UITableViewCell
時,常常遇到須要清空全部subview的狀況。而咱們有這樣一段代碼能夠利用:程序員
- (void)removeAllSubviews { for (UIView *subView in self.subviews) { [subView removeFromSuperview]; } }
正確的作法是,把全部的子 view 都加在 contentView
上。測試
[self.contentView removeAllSubviews]; // …… [self.contentView addSubview:view];
錯誤的作法是,把子 view 加在 cell 自己的 view 上。code
[self removeAllSubviews]; // …… [self addSubview:view];
這樣在removeAllSubviews
時,不只 remove 掉了本身添加的那些子 view,也一同 remove 掉了UITableViewCell
的contentView
。rem
錯誤的作法形成的後果是:在 iOS 7(測試用版本爲7.1)上,cell 顯示爲一片空白。全部的 subview 都顯示不出來。在 iOS 6 和 iOS 8 上均正常。bug
UITableViewCell
的contentView