- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 iOS 开发的新手,我正在尝试将我在 Android 上的项目移植到 iOS。我正在尝试在代码中构建一个 collectionView 作为实现的一部分。事情是因为 Collection View 中每个单元格中的数据可以是不同的字符串大小,每个单元格可以与其相邻单元格的大小不同。我正在试验附加的代码,但我注意到无论是物理设备还是模拟器中的 iPhone 6,用户界面看起来都符合预期……其他任何东西看起来都非常糟糕。似乎是 sizeForIndexItemAtPath 破坏了它。有什么想法吗?时间差
import UIKit
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var collectionView: UICollectionView?
var thisCellHeight: CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
var thisWidth = screenWidth - 10
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 70, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: thisWidth, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
//collectionView!.backgroundView?.backgroundColor = UIColor.blueColor()
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView!.backgroundColor = hexStringToUIColor("#15ADFF")
self.view.addSubview(collectionView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.whiteColor()
var typeLabel = UILabel(frame: CGRectMake(10, 0, 200, 21))
typeLabel.text = "Card Type"
typeLabel.font = typeLabel.font.fontWithSize(20)
cell.addSubview(typeLabel)
var titleLabel = UILabel(frame: CGRectMake(10, 25, 300, 21))
titleLabel.text = "This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title."
titleLabel.font = titleLabel.font.fontWithSize(20)
titleLabel.numberOfLines = 0
titleLabel.sizeToFit()
self.thisCellHeight = CGFloat(titleLabel.frame.height)
println("The title text is \(self.thisCellHeight) high")
cell.addSubview(titleLabel)
var authorLabel = UILabel(frame: CGRectMake(10, thisCellHeight + 30, 200, 21))
authorLabel.text = "This Is The Article Author"
authorLabel.font = authorLabel.font.fontWithSize(15)
cell.addSubview(authorLabel)
var timestampLabel = UILabel(frame: CGRectMake(10, thisCellHeight + 50, 200, 21))
timestampLabel.text = "This Is The Timestamp"
timestampLabel.font = timestampLabel.font.fontWithSize(15)
cell.addSubview(timestampLabel)
return cell
}
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString
if (cString.hasPrefix("#")) {
cString = cString.substringFromIndex(advance(cString.startIndex, 1))
}
if (countElements(cString) != 6) {
return UIColor.grayColor()
}
var rgbValue:UInt32 = 0
NSScanner(string: cString).scanHexInt(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(320, 350)
}
}
最佳答案
您将布局上的项目大小设置为:
layout.itemSize = CGSize(width: thisWidth, height: 120)
而且你也在使用委托(delegate)方法
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(320, 350)
}
第一个被第二个覆盖。 layout.itemSize
是每个项目的默认大小,前提是委托(delegate)方法 sizeForItemAtIndexPath
未被覆盖。您正在用看起来完全不同的东西覆盖 layout.itemSize
。尝试删除委托(delegate)函数 sizeForItemAtIndexPath
,这是否符合您的预期?
关于iOS collectionView.sizeForItemAtIndexPath 在 iPhone 6 之前中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28625990/
关于模型- View - Controller (MVC) 的一个非常基本的问题。 我有一个自定义 UICollectionView,其中填充了自定义 UICollectionViewCells。 C
我已经让我的 Collection View 单元格以编程方式计算,并且我正在使用 sizeForItemAtIndexPath 方法来设置单元格的大小。 但是当屏幕旋转时,我需要重新计算尺寸并为单元
我有一个带有 UICollectionViewFlowLayout 的 UICollectionView。在使用大数据(5,000 多个单元格)进行测试时,我注意到加载时存在延迟。我发现在加载时,每个
这让我发疯!我有一个 UICollectionViewController,如下所示: class PhrasesCompactCollectionViewController: UICollecti
在过去的 5 天里,我一直在为这个问题苦苦挣扎,无法取得任何重大突破。 背景:我有一个带有自定义布局的 UICollectionView。 UICollectionView 有一个 textView
我试图在 UICollectionViewCellCell 中创建一个 UICollectionView,但是函数 sizeForItemAtIndexPath 和 cellForItemAtInde
所以很有趣,我正在尝试使用 collectionViewLayout sizeForItemAtIndexPath 在 collectionView 中一次只显示一个项目并水平滚动,我试过这个: -
我正在尝试实现 UICollectionViewDelegateFlowlayout 的 sizeForItemAtIndexPath,类似于本教程:http://www.raywenderlich.
我想为调整 UICollectionViewCell 的大小设置动画。我已经编写了下面的代码,但不能在动画 block 中包含 return 行。有什么想法吗? func collectionView
如果我有一个具有自动布局的单元格,我正在循环从哪里获取 Collection View 中单元格的大小 我知道 sizeForItemAtIndexPath 应该从数据而不是单元格本身获取大小,因为单
我正在使用 UICollectionView 但未找到以下方法: func collectionView(collectionView: UICollectionView, layout colle
我有一个包含不同自定义单元格的 Collection View 。 这些单元格包含大小不同的不同内容。 单元格在 Storyboard 中定义,因此无需在代码中注册。 我现在要做的就是根据内容更改委托
我有以下代码,它将 Collection View 放入一列或两列中,具体取决于屏幕的大小: func collectionView(collectionView: UICollectionView,
我在具有两个 Collection View 的 View Controller 中使用 sizeForItemAtIndexPath 函数。 如何在 sizeForItemAtIndexPath 函
我能够在一个非常简单的独立应用程序上重现它。 我有一个 collectionView,我想制作循环/循环,以便元素一次又一次地重复(也就是当用户位于数组中的最后一个元素时,它会再次显示第一个元素。如果
我希望能够在 sizeForItemAtIndexPath 函数中调用我的 UICollectionViewCell 类。像这样: func collectionView(_ collectionVi
我正在使用 collectionView 来显示从 Coredatabase 获取的图像。但是,除 numberOfItemsInSection 和 numberOfSectionsInCollect
当我调用以下委托(delegate)方法时: - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollect
我在我的 UICollectionView 和我的 sizeForItemAtIndexPath 函数中有可变宽度,我最终返回一个 CGSize func collectionView(_ colle
我是 iOS 开发的新手,我正在尝试将我在 Android 上的项目移植到 iOS。我正在尝试在代码中构建一个 collectionView 作为实现的一部分。事情是因为 Collection Vie
我是一名优秀的程序员,十分优秀!