【问题标题】:UICollectionView horizontal cellUICollectionView 水平单元格
【发布时间】:2014-12-05 11:08:38
【问题描述】:

我正在尝试创建一个包含多个单元格的视图集合。

第一个单元格必须包含时间线。

正如您在图像中看到的,无法水平打印时间线。

如何水平打印此单元格?

我的代码:

      class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var year = 2000
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 200
     }

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: 97, height: 33)

}


     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MyViewCell

        cell.textView.text = "\(year)";
        year++;

        return cell
    }
}

【问题讨论】:

  • 所有单元格水平?
  • 并非如此。一共三个单元格,只有两个水平的和一个垂直的。在这个链接中:你可以看到我在做什么:code4app.net/ios/Static-column-table/50fe649d6803fa7b65000001
  • 减小单元格大小,使两个单元格进入水平宽度。并在部分中分配三个单元格。
  • noOfYears/3 应该是你的部分。
  • 对不起,我不明白。为什么要缩小细胞大小?我只是希望单元格在水平方向一个接一个地打印

标签: ios swift uicollectionview


【解决方案1】:

默认的集合视图布局应该已经像这样工作了(来自 Apple Docs):

这是您为每个单元格设置年份的方式。不要为此使用year++。让它回复indexPath.row:

cell.textView.text = "\(2000 + indexPath.row)";

这里还有一个很棒的教程:UICollectionView Swift Tutorial

【讨论】:

  • 好的,但我希望单元格不换行。我想创建一个时间线并随着水平滚动移动
  • 好的,那么你需要改变 UICollectionViewFlowLayout 的滚动方向,改变单元格/集合视图的高度,只允许 1 行
  • 如何改变单元格的高度?我曾尝试更改情节提要,然后单击 CollectionView 单元格大小并降低高度。但它不起作用
  • 您应该实现 UICollectionViewDelegateFlowLayout 并在 collectionView:layout:sizeForItemAtIndexPath: 中提供正确的大小:(参见此处:developer.apple.com/library/ios/documentation/uikit/reference/…
  • 但是不需要实现方法:sizeForItemAtIndexPath。项目的大小也可以从情节提要中更改。但我不明白为什么要在一行中打印更改大小
猜你喜欢
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多