在运行时更改集合视图单元格的宽度 [英] Change collection view cell width at runtime

查看:46
本文介绍了在运行时更改集合视图单元格的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我基于一个很棒的示例构建它:



然后在 tableView 委托 collectionView

  func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath)-> UITableViewCell 
{
让单元格:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier( cell,forIndexPath:indexPath)为! CustomTableViewCell

cell.collectionView.delegate =自我
cell.collectionView.dataSource =自我
cell.collectionView.tag = indexPath.row

单元格标签= 100 + indexPath.row;

返回单元格;
}

委派后,方法 collectionView:sizeForItemAtIndexPath 被调用,在此方法上,您必须使用indexPath计算宽度或高度检索数据

  func collectionView(collectionView :UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath)-> CGSize 
{
//根据indexPath

// ...
返回宽度CGSizeMake(width,89)
}

最后,要同时使所有单元格的滚动均匀,请使用方法 scrollViewDidScroll 。检查此帖子:水平滚动UICollectionView的所有行在一起

  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;
对于var i = 0;我< data.count; i ++
{
let cell = self.view.viewWithTag(100 + i)as? ChannelTableViewCell;

if(cell!= nil)
{
让collectionCell:UICollectionView吗? = cell?.collectionView;

if(collectionCell!= nil)
{
collectionCell!.contentOffset = scrollView.contentOffset;
}
}
}
}
}


I'm creating an EPG (Electronic Program Guide); to retrieve the program info I use JSON. For the design, I have a table view in the left half for the channel logo and name, and in the right half a collection view for all the TV programs for that channel. The thing is that for every show the collection view row must have a different width that depends on the duration of the show.

I'm building it based on this fantastic example called: EPG Grid

But in this example, all the channels are loaded previously in one array. This works fine if the channels list is short, but in my case the list is very large, so I need to load every channel in tableView:cellForRowAtIndexPath:, then create the layout with the specified width.

Any thoughts about this?

解决方案

I figured out by my self, the thing is in my case not to use UICollectionViewLayout (CustomLayout) use layout Flow and add the collectionView inside tableView like this:

Then in every row of tableView delegate the 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;
}

After delegate, the method collectionView:sizeForItemAtIndexPath is called, on this method you must to calculate width or height retrieving data using indexPath

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

    //...
    return CGSizeMake(width, 89)
}

Finally for making the scroll uniform for all the cells at the same time use the method scrollViewDidScroll. Check this post: 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;
                }
            }
        }
    }
}

这篇关于在运行时更改集合视图单元格的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆