我是iOS的新手,现在我遇到了一个问题。
我的目标是获得一个带有过滤按钮的滑动条。 我有一个UICollectionView作为其他视图中的子视图,除了最后一个单元格外,它们都显示良好。
这是uicollectionview
的委托和日期来源class PmtCategoryCollectionViewController: UICollectionViewController {
var categories = ["Mediterranean", "Italian", "French", "Chinese", "American", "Indian"]
var buttonDistanceToEachOther: CGSize = CGSize(width: 20, height: 60)
var fontOnCategoryButton = UIFont.systemFontOfSize(15.0)
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
println(categories.count)
return categories.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PmtCategoryCollectionViewCell
// Configure the cell
println(categories[indexPath.row])
cell.category = categories[indexPath.row]
return cell
}
// determine the size of each cell
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var text = categories[indexPath.row]
var size: CGSize = NSString.sizeWithAttributes(text)([NSFontAttributeName:fontOnCategoryButton])
size.height += buttonDistanceToEachOther.height
size.width += buttonDistanceToEachOther.width
return size
}
}
所有细胞的大小是:
Size is (118.91, 72.895)
Size is (37.52, 72.895)
Size is (60.77, 72.895)
Size is (67.025, 72.895)
Size is (75.5, 72.895)
Size is (84.545, 72.895)
Size is (61.745, 72.895)
Size is (176.21, 72.895)
任何帮助都会很棒!
答案 0 :(得分:1)
我有同样的问题,在我的情况下,帮助只是从UI Builder中的 UICollectionView 中删除原型单元项目,并通过创建xib并注册它来使用 UICollectionViewCell xib作为 viewDidLoad 中的单元格视图nib:
[collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"];
以及后来的 cellForItemAtIndexPath :
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCellIdentifier" forIndexPath:indexPath];
答案 1 :(得分:-2)
有时,根据我自己的经验,iOS无法正确对齐UICollectionView上的最后一个单元格。
为避免这种情况,您可以在dataSource的最后位置添加一个新单元格,并将其高度设置为0.使用该解决方案',错误对齐的单元格将是最后一个单元格对你来说并不重要,在实践中,细胞将是不可见的。