简体   繁体   English

Swift:设置model后修改collectionViewCell中的UIStackView

[英]Swift: Modify UIStackView in collectionViewCell after model is set

I have a UIStackView inside a collectionViewCell that I would like to modify depending on the Tweet object. If the Tweet object contains a retweet object, I would like to include the retweetedTextView to the stackView.我在 collectionViewCell 中有一个 UIStackView,我想根据Tweet文 object 对其进行修改。如果Tweet文 object 包含retweet object,我想将retweetedTextView包含到 stackView。

My current implementation doesn't render the stackView on screen at all.我当前的实现根本不在屏幕上呈现 stackView。 Code:代码:

class TweetCell: UICollectionViewCell {
    
    //Initialize UI elements
    
    var tweet: Tweet? {
        didSet {
            if let tweet = tweet {
                
                if let _ = tweet.retweetedStatus {
                    bottomHStack = VStack(subviews: [
                        reactionHStack,
                        isRetweetedTextView,
                        tweetTextTextView,
                        timestampLabel
                    ], spacing: 8, layoutMargins: UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing))
                } else {
                    bottomHStack = VStack(subviews: [
                        reactionHStack,
                        tweetTextTextView,
                        timestampLabel
                    ], spacing: 8, layoutMargins: UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing))
                }   
            }
        }
    }
    
    var bottomHStack: VStack!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func setupViews() {

        //Other stackviews 
        
        bottomHStack = VStack(subviews: [
            reactionHStack,
            isRetweetedTextView,
            tweetTextTextView,
            timestampLabel
        ], spacing: 8, layoutMargins: UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing))
        
        //Overall stack
        let mainStack = VStack(subviews: [
            userHStack,
            mediaCollectionView,
            bottomHStack,
            separatorLine
        ])
        
        addSubview(mainStack)
        mainStack.topAnchor.constraint(equalTo: topAnchor).isActive = true
        mainStack.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        mainStack.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        mainStack.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    } 

I have considered alternative ways to achieve the "removal of isRetweetedTextView ", but doesn't seem to be optimal, for example setting the heightConstraint to zero.我已经考虑过实现“删除isRetweetedTextView ”的替代方法,但似乎不是最佳方法,例如将 heightConstraint 设置为零。 Although it does appear to remove the isRetweetedTextView , the spacing in the stackView is still present.虽然它似乎确实删除了isRetweetedTextView ,但 stackView 中的间距仍然存在。

I would like to completely remove the view from the stack.我想从堆栈中完全删除视图。

Note that I am also implementing a similar feature to tweetTextTextView , ie if the tweetText is an empty string, I would like to remove it from the stackView as well.请注意,我还实现了与tweetTextTextView类似的功能,即如果 tweetText 是空字符串,我也想将其从 stackView 中删除。 Creating different stackviews to cater for the varying combinations would be impractical.创建不同的堆栈视图来满足不同的组合是不切实际的。

There's a default behavior in UIStackView that it automatically removes a view and rearrange the stack if that view's isHidden property is true . UIStackView中有一个默认行为,如果该视图的isHidden属性为true ,它会自动删除视图并重新排列堆栈。

You could create one stack view with all possible views on it, then simply hide or show the views based on your needs.您可以创建一个包含所有可能视图的堆栈视图,然后根据您的需要简单地隐藏或显示视图。 There is no need to initialize multiple stack views for the different possible arrangements of their underlying views.无需为底层视图的不同可能安排初始化多个堆栈视图。

For example, you could initialize one stack view with reactionHStack , isRetweetedTextView , tweetTextTextView , and timestampLabel , then make isRetweetedTextView.isHidden = true if there is no retweet.例如,您可以使用reactionHStackisRetweetedTextViewtweetTextTextViewtimestampLabel初始化一个堆栈视图,然后如果没有转推则使isRetweetedTextView.isHidden = true

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

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