2016-03-14 81 views
0

我正在創建EPG(電子程序指南);檢索我使用JSON的程序信息。對於設計,我在左半部分有一個表格視圖,用於頻道標識和名稱,右半部分是該頻道所有電視節目的收藏視圖。事情是,對於每個節目,集合視圖行必須具有不同的寬度,這取決於節目的持續時間。在運行時更改集合視圖單元格寬度

我在此基礎上稱之爲夢幻般的例子構建它:EPG Grid

但在這個例子中,所有的通道都在一個陣列之前加載的。如果通道列表很短,這可以正常工作,但在我的情況下,列表非常大,所以我需要在tableView:cellForRowAtIndexPath:中加載每個通道,然後創建具有指定寬度的佈局。

對此有何想法?

回答

0

我用我自己想通了,事情是在我的情況不使用UICollectionViewLayout(CustomLayout)使用佈局Flow並添加collectionViewtableView這樣的:

enter image description hereenter image description here

然後每行中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; 
} 

代表之後,該方法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; 
       } 
      } 
     } 
    } 
} 
相關問題