問題
別人建的Cell需要多選刪除時出現(xiàn)UITableViewCellEditControl
被遮擋的情況,如下圖:
原因
經(jīng)過排查發(fā)現(xiàn)時Cell布局代碼有問題:
原來的代碼在 -initWithStyle:reuseIdentifier:
方法中添加 subView
俄周,在 layoutSubviews
中對子控件進(jìn)行布局吁讨, 并在設(shè)置 model
的時候調(diào)用 [self layoutIfNeeded];
并觸發(fā) layoutSubviews
。
原代碼沒有調(diào)用[super layoutSubviews];
解決
經(jīng)過把 layoutSubviews
中對子控件的布局移動到 layoutSubviews
方法后峦朗,問題解決建丧。
layoutSubviews
的使用和調(diào)用時機(jī)
參考文檔:
如果子控件的布局約束無法滿足需求,需要對子控件更改布局時可以重寫layoutSubviews
,可以直接設(shè)置子控件的 frame 波势。
不能直接調(diào)用layoutSubviews
翎朱。 如果要強(qiáng)制進(jìn)行布局更新,請在下次繪圖更新之前調(diào)用 setNeedsLayout
方法尺铣。 如果要立即更新視圖的布局拴曲,請調(diào)用layoutIfNeeded
方法。
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call thesetNeedsLayoutmethod instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.
調(diào)用時機(jī)
1凛忿、init
初始化不會觸發(fā)layoutSubviews
澈灼;
2、addSubview
會觸發(fā)layoutSubviews
店溢,如果添加的子控件沒有 Frame ,不會調(diào)用叁熔;
3、設(shè)置 view 的 Frame 會觸發(fā)layoutSubviews
逞怨,當(dāng)然前提是frame的值設(shè)置前后發(fā)生了變化且該 View 已經(jīng)被添加到父控件者疤;
4、滾動一個 UIScrollView
會觸發(fā) layoutSubviews
,因滾動 UIScrollView
,其子控件肯定對應(yīng)會刷新,也就肯定會被調(diào)用叠赦;
5驹马、旋轉(zhuǎn) Screen 會觸發(fā)控制器對應(yīng) UIView
上的 layoutSubviews
事件;
總結(jié):改變子控件就會調(diào)用父類的方法除秀;
注:在一個方法內(nèi)連續(xù)調(diào)(主動調(diào)用或者系統(tǒng)隱式調(diào)用)糯累,都只會調(diào)一次layoutSubviews;