简体   繁体   English

从单元中删除子视图以解决自动布局问题

[英]Removing subview from cell for auto layout issue

What is the best way to remove auto layout constraints for a Cell when a view should not be shown? 当不应显示视图时,删除单元格自动布局约束的最佳方法是什么?

We have a cell which has a layout with around 6-7 views. 我们有一个单元格,其布局具有大约6-7个视图。 One of those views is for a star rating. 这些观点之一就是星级评价。 When the star rating is not available we do not want to show the view. 当星级不可用时,我们不想显示视图。 At present we hide the view but this leaves the auto layout constraints in place. 目前,我们隐藏了视图,但这将自动布局约束保留在适当的位置。

Similar question - How to use auto-layout to move other views when a view is hidden? 类似的问题- 隐藏视图时如何使用自动布局来移动其他视图?

在此处输入图片说明

This is the view in question hilighted above. 这是上面有问题的视图。 We would ideally like to remove this view from its superview when there is no available star rating. 理想情况下,当没有可用的星级时,我们希望将此视图从其超级视图中删除。 The issue we have is that if we remove the view from superview removeFromSuperview in cellForRow... then the next cell would be affected, because the view is not added again. 我们遇到的问题是,如果我们从cellForRow... superview removeFromSuperview中删除视图,则下一个单元格将受到影响,因为不会再次添加视图。

(I would comment to request clarification, but do not yet have the reputation.) (我想发表评论以要求澄清,但还没有声誉。)

Is there a chance you could just hide the view for the cell in question? 您是否有可能只隐藏有关单元的视图?

If I understand what you are saying, you are suggesting that removing the view in question from it's superview creates an issue when you are creating a new cell. 如果我理解您的意思,则建议您从超级视图中删除相关视图会在创建新单元格时产生问题。 So, when you dequeue a new cell, just check your star-count property, and if it is >0 for this next cell, then show the view for that cell. 因此,当您将一个新单元格出队时,只需检查您的star-count属性,如果下一个单元格的属性大于0,则显示该单元格的视图。

Another option is to pin the surrounding views to their parent view rather than pinning them to this view that you want to remove. 另一个选择是将周围的视图固定到其父视图,而不是将其固定到要删除的视图。 This way, when you remove the star-rating view, the layout constraints for the surrounding views remain unchanged. 这样,当您删除星级视图时,周围视图的布局约束将保持不变。

remember the constraint (for show/hide a view) in to variable and remove it constrains. 记住变量的约束(用于显示/隐藏视图)并删除它的约束。 and delete this constrains and replace with new constrains if you need change view. 并删除此约束,并在需要更改视图时替换为新约束。

For example: 例如:

view has width and height for show and replace constraint where width and height will be zero for hide view. 视图具有widthheight为展示和替换约束,其中widthheight将是隐藏视图为零。

The best route I found was to create separate cell layouts and decide which layout to use based on if the information was available. 我发现的最佳途径是创建单独的单元布局,并根据信息是否可用来决定使用哪种布局。 This meant creating a second prototype cell in IB without the view in question and different constraints but works as expected. 这意味着在IB中创建第二个原型单元,而无需考虑相关视图和不同限制,但可以按预期工作。 Open to other suggestions on this one. 对此开放其他建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM