首页 > 代码库 > 如新闻频道滑动切换的代码

如新闻频道滑动切换的代码

效果:

技术分享


collectionView的数据源方法

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    newsCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"news"forIndexPath:indexPath];

    NSString *urlString =self.arrayList[indexPath.item];

    if (![self.childViewControllerscontainsObject:cell.newsVc]) {

        [selfaddChildViewController:(UIViewController *)cell.newsVc];

    }

    cell.urlStr = urlString;

    return cell;

}


cell的自己定义例如以下

- (void)setUrlStr:(NSString *)urlStr

{

    _urlStr = urlStr;

    self.newsVc.urlStr = urlStr;

}

- (void)awakeFromNib

{

    UIStoryboard *sb = [UIStoryboardstoryboardWithName:@"news"bundle:nil];

    self.newsVc = sb.instantiateInitialViewController;

    // 设置 view frame,否则,view frame的大小不会调整!

    self.newsVc.view.frame =self.bounds;

    

    // vc的视图加入到 cell

    [selfaddSubview:self.newsVc.view];

}


如新闻频道滑动切换的代码