簡體   English   中英

為什么UICollectionView中的單元格被切除?

[英]Why are cells in the UICollectionView being cut off?

由於某些奇怪的原因,我的UICollectionView中的第一個單元格被切斷了。 我使用Interface Builder中的原型單元設計了一個單元。 我設置了一個自定義的高度和約束,但是,在運行該應用程序時,該單元格被切斷了。 請查看下面的圖片進行說明:

它應該是這樣的:

在此處輸入圖片說明

這是運行應用程序時的外觀:

在此處輸入圖片說明

這是集合視圖所在的類的代碼:

override func viewDidLoad() {
    super.viewDidLoad()

    // set the navigation title
    self.navBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Trueno", size: 17)!]
    self.navBar.topItem?.title = user.username

}

@IBAction func handleSettings(_ sender: Any) {

    let action = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)


    let settings = UIAlertAction(title: "Settings", style: .default) { action -> Void in
        self.show(ViewController: "SettingScene")
    }
    let editProfile = UIAlertAction(title: "Edit Profile", style: .default) { action -> Void in
        self.show(ViewController: "SettingScene")
    }
    let cancel = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in}

    action.addAction(editProfile)
    editProfile.setValue(UIColor.purple, forKey: "titleTextColor")
    action.addAction(settings)
    settings.setValue(UIColor.purple, forKey: "titleTextColor")
    action.addAction(cancel)
    cancel.setValue(UIColor.purple, forKey: "titleTextColor")

    present(action, animated: true, completion: nil)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return user.posts.count + 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    var cell: UICollectionViewCell

    // add the profile header/info cell if there are no cells inside of the colelction view
    if collectionView.visibleCells.count == 0 {
        let c = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileInfoCollectionViewCell", for: indexPath) as! ProfileInfoCollectionViewCell

        c.profilePic.layer.cornerRadius = c.profilePic.frame.width / 2
        c.profilePic.image = user.profilePic
        c.profilePic.clipsToBounds = true

        c.name.text = user.name
        c.bio.text = user.bio
        c.followerCount.text = String(user.followers.count)
        c.followingCount.text = String(user.following.count)
        c.memeCount.text = String(user.postCount)

        cell = c
    } else {
        let c = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfilePostCollectionViewCell", for: indexPath) as! ProfilePostCollectionViewCell
        c.imageView.image = user.posts[indexPath.row - 1].img
        cell = c
    }

    return cell

}

任何解決此問題的幫助將不勝感激。 謝謝!

您可以調用sizeForItemAtIndexPath委托方法來設置單元格的大小。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    // Set the size for cell
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM