【问题标题】:Change collection view cell width at runtime在运行时更改集合视图单元格宽度
【发布时间】:2016-06-30 00:22:55
【问题描述】:

我正在创建 EPG(电子节目指南);检索我使用 JSON 的程序信息。对于设计,我在左半部分有一个用于频道徽标和名称的表格视图,在右半部分有一个用于该频道所有电视节目的集合视图。问题是对于每个节目,集合视图行必须具有不同的宽度,具体取决于节目的持续时间。

我正在基于这个名为:EPG Grid的奇妙示例构建它

但在本例中,所有通道都预先加载到一个数组中。如果频道列表很短,这可以正常工作,但在我的情况下,列表非常大,所以我需要加载tableView:cellForRowAtIndexPath: 中的每个频道,然后创建具有指定宽度的布局。

对此有什么想法吗?

【问题讨论】:

    标签: ios cocoa-touch uicollectionview


    【解决方案1】:

    这是 Swift 5 唯一对我有用的东西,我希望它有所帮助。

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) 
    {
         super.viewWillTransition(to: size, with: coordinator)
                
         collectionView.collectionViewLayout.invalidateLayout()
         collectionView.frame.size = size
    }
    

    【讨论】:

      【解决方案2】:

      我自己想通了,在我的情况下,不要使用UICollectionViewLayout (CustomLayout) 使用布局Flow 并在tableView 中添加collectionView,如下所示:

      然后在tableView 的每一行中委托collectionView

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
      {        
          let cell: CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
      
          cell.collectionView.delegate = self
          cell.collectionView.dataSource = self
          cell.collectionView.tag = indexPath.row
      
          cell.tag = 100 + indexPath.row;
      
          return cell;
      }
      

      delegate之后,调用collectionView:sizeForItemAtIndexPath方法,在该方法上必须使用indexPath计算宽度或高度检索数据

      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 
      {
          //Calculation of width based on indexPath
      
          //...
          return CGSizeMake(width, 89)
      }
      

      最后,为了使所有单元格同时滚动统一,请使用方法scrollViewDidScroll。查看此帖子:Horizontally scroll ALL rows of UICollectionView together

      func scrollViewDidScroll(scrollView: UIScrollView) {
      
          var ret: Bool = false;
      
          if (self.lastContentOffset > scrollView.contentOffset.x)
          {
              ret = true;
          }
          else if (self.lastContentOffset < scrollView.contentOffset.x)
          {
              ret = true;
          }        
      
          if(scrollView.isKindOfClass(UICollectionView) && ret)
          {
              self.lastContentOffset = scrollView.contentOffset.x;
              for var i = 0; i < data.count; i++
              {
                  let cell = self.view.viewWithTag(100 + i) as? ChannelTableViewCell;
      
                  if(cell != nil)
                  {
                      let collectionCell: UICollectionView? = cell?.collectionView;
      
                      if(collectionCell != nil)
                      {
                          collectionCell!.contentOffset = scrollView.contentOffset;
                      }
                  }
              }
          }
      }
      

      【讨论】:

      • 嗨,你能分享你的项目吗,我正在使用 swift 实现同样的事情,而且传递数据有点卡住,我正在使用 NSFetchedResultsControllerDelegate,但不了解何时以及如何将数据发送到流布局
      • @garanda 我面临同样的问题。你找到解决办法了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多