在操作表格顯示時,會需要把特定欄位隱藏,最簡單的方式就是把row的高度設定成0

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

使用Xib建立TableViewCell作業時,直接把heightForRowAtIndexPath設為0時。雖然顯示高度為0但TableViewCell中的Label依然看的到

原來是Clips To Bounds造成的問題。使用Storyboard建立的TableViewCell預設會開啟 Clips To Bounds,而(自動建立)Xib預設是沒有開的

 

解決方式如下:

[方法一]在程式中設定(programming method):

cellForRowAtIndexPath 中增加設定cell的Clips To Bounds為YES

[cell setClipsToBounds:YES];

 

[方法二]在Xib(或Storyboard)設定中開啟Clips To Bounds

 

設定完成後就能成功隱藏該Row了

補充:

clipsToBounds是一個布林值,用來決定子視圖是否裁切超出父容器中邊界的畫面

若設定為YES表示會裁切,因此本例中當heightForRowAtIndexPath(父容器)設定為0時,子視圖(也就是文字label)就會被裁切看不到;反之,在沒有裁切的情況下就會造成這次的問題。

 

參考資料:

stackoverflow

相关文章