ios - 仅调用一个 UICollectionViewCell 的函数

标签 ios swift nslayoutconstraint

<分区>

我有自定义 UICollectionViewCell,这里是 cellForItemAt: 方法:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
    if let CurrentPost = posts[indexPath.row] as? Post{
        //determine which constraint to call
        if(CurrentPost.PostText != nil){
            if(CurrentPost.PostImage != nil){
                cell.postImage.image = CurrentPost.PostImage
                cell.cellConstraintsWithImageWithText()
            }else{
                cell.postImage.image = nil
                cell.cellConstraintsWithoutImageWithText()
            }
        }else{
            cell.postImage.image = CurrentPost.PostImage
            cell.cellConstraintsWithImageWithoutText()
        }
    }
    return cell
}

我想根据 UIImage 的缺失或存在来调用 constraint 函数,但是当调用一个函数时,它会保持不变,所以所有的单元格都会弄乱,因为一个单元格是向上滚动时被重新使用。

最佳答案

PostCell 类的 prepareForReuse 方法中,您需要将约束恢复到原始状态

像这样

override func prepareForReuse() {
        super.prepareForReuse()
        self.yourConstraint.constant = originalValue //this is an example
    }

希望对你有帮助

关于ios - 仅调用一个 UICollectionViewCell 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485286/

相关文章:

ios - 如何根据 UITableView 中的单元格数量更改 UITableViewCell 内 UItableView 的高度

ios - IBDesignables 的某种 "global"?

ios - 应用商店问题中的应用拒绝?

ios - 向 UILabel 添加粗体

ios - 添加启动屏幕 Storyboard后 Xamarin iOS 布局发生变化

ios - 使用 XCTest 点击 UI 元素时如何避免 NSInternalInconsistencyException?

objective-c - 在Objective C中子类化时需要实现的方法

swift - 打印可选变量

javascript - 尝试在 XCTest 中进行模板测试

ios - 自动布局+基于总行数的动态表格高度约束