如新聞頻道滑動切換的代碼,新聞頻道滑動切換
效果:
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];
}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。