ios - 为什么 UICollectionView 中的单元格被截断?
问题描述
出于某种奇怪的原因,我的 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
}
推荐阅读
- puppet - 在 puppet 中选择一个具有给定键值对的嵌套哈希
- hyperledger-composer - 使用超级账本作曲家启动业务网络时出错
- sql - 将一列中的字符串拆分为表维护 ID 的多个分隔符
- php - 与 laravel-datatable 中的排序和分页功能冲突
- c# - 创建一个接受具有返回类型的函数的动作
- python - Plotly 和袖扣在 Anaconda 中不起作用
- function - 函数是如何工作的
- xamarin - 在 Xamarin 移动应用程序中使用 Azure B2C 和 MSAL 对 Web Api 进行身份验证
- live-wallpaper - 动态壁纸问题
- python - python中的键盘输入数据类型