如新聞頻道滑動切換的代碼

效果:


collectionView的數據源方法

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

{

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

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

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

        [self addChildViewController:(UIViewController *)cell.newsVc];

    }

    cell.urlStr = urlString;

    return cell;

}


cell的自定義如下

- (void)setUrlStr:(NSString *)urlStr

{

    _urlStr = urlStr;

    self.newsVc.urlStr = urlStr;

}

- (void)awakeFromNib

{

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

    self.newsVc = sb.instantiateInitialViewController;

    // 設置 view frame,否則,view frame 的大小不會調整!

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

    

    // vc 的視圖添加到 cell

    [self addSubview:self.newsVc.view];

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章