2017-02-17 43 views
1

我想要一個具有5個CollectionView的TableView。集合視圖必須顯示水平滾動的圖像。我遵循本教程:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/CollectionView與TableViewCell中的圖像

但我想要在我的CollectionView中滾動的圖像。我不知道該怎麼做。

這是我的示例代碼:

import UIKit 

    class ViewController1: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegateFlowLayout { 

    @IBOutlet var tableView: UITableView! 

    var images = [UIImage(named:"banner2"),UIImage(named:"banner1"),UIImage(named:"banner3"),UIImage(named:"banner4"),UIImage(named:"banner5")] 

    var storedOffsets = [Int: CGFloat]() 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return 5 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2 
      cell.frame = CGRect(x: 0, y: 28, width: 375, height: 202) 
      cell.myCollection.reloadData() 
      return cell 
     } 

     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
       guard let tableViewCell = cell as? Cell2 else { return } 
       tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) 
       tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 
      } 

     func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
      guard let tableViewCell = cell as? Cell2 else { return } 
      storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset 
     } 
    } 

extension ViewController1: UICollectionViewDelegate, UICollectionViewDataSource { 

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
      return images.count 
     } 

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! Cell1 
      cell.image.image = images[(indexPath as NSIndexPath).row]  
      return cell 
     } 

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
      print("Collection view at row \(collectionView.tag) selected index path \(indexPath)") 
     } 
    } 


import UIKit 
class Cell1: UICollectionViewCell { 

     @IBOutlet var image: UIImageView! 

     override init(frame: CGRect) { 
      super.init(frame: frame) 
       image.frame = CGRect(x: 0, y: 0, width: 175, height: 175) // Here I get exc_bad_instruction (code=exc_i386_invop subcode=0x0) warning 
       contentView.backgroundColor = UIColor.green 
      self.contentView.addSubview(image) 
     } 

     required init?(coder aDecoder: NSCoder) { 
      super.init(coder: aDecoder) 
     } 
    } 

import UIKit 

class Cell2: UITableViewCell { 

     @IBOutlet var myCollection: UICollectionView! 

     override func awakeFromNib() { 
      myCollection.frame = CGRect(x: 0, y: 28, width: 375, height: 202) 
     } 
    } 

extension Cell2 { 

      func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) { 

       self.myCollection.delegate = dataSourceDelegate 
       self.myCollection.dataSource = dataSourceDelegate 
       myCollection.tag = row 
       myCollection.setContentOffset(myCollection.contentOffset, animated:false) // Stops collection view if it was scrolling. 
        let layout = UICollectionViewFlowLayout.init() 
        layout.scrollDirection = .horizontal 
        layout.sectionInset = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0) 
        layout.minimumInteritemSpacing = 0 
        layout.minimumLineSpacing = 0 
        layout.itemSize = CGSize(width: 182, height: 182) 
        myCollection = UICollectionView.init(frame: myCollection.frame, collectionViewLayout: layout) 
        myCollection.register(Cell1.self, forCellWithReuseIdentifier: "cell1") 
        self.contentView.addSubview(myCollection) 
       myCollection.reloadData() 
      } 

var collectionViewOffset: CGFloat { 
    set { myCollection.contentOffset.x = newValue } 
    get { return myCollection.contentOffset.x } 
    } 
} 

My output is like this

粉紅的TableView(I設置顏色)和黑色的CollectionView(同樣,我設置的顏色)我無法查看我的圖片或我設置爲greenColor的CollectionViewCells。當觸摸在粉紅色的部分或嘗試滾動我得到的錯誤:致命錯誤:意外發現零,而與警告展開一個可選值 - exc_bad_instruction(代碼= exc_i386_invop子碼=爲0x0)

誰能告訴我,我怎麼能做到這一點?在此先感謝

我使用的Xcode 8和斯威夫特3.

+0

您需要實現'UICollectionViewDataSorce'和委託方法與'UITableViewCell'不能以'ViewController1'。 –

+0

是的,我試圖做到這一點。它給了我相同的輸出 –

回答

0

我在OBJECTIVE-Ç片斷,我認爲你必須轉換爲SWIFT 3.0

這裏是我的代碼

你可以試試吧

的TableCell的cellForRowAtIndexPath

static NSString *CellIdentifier = @"cell3"; 
     CollectionViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[CollectionViewTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     cell.headerLabel.text = @"DISH TYPE"; 

     cell.collectionView.delegate = self; 
     cell.collectionView.dataSource = self; 
     cell.collectionView.tag = indexPath.row; 
     [cell.collectionView reloadData]; 

和集合視圖cellForItemAtIndexPath

if(collectionView.tag == 4){ 
     FIPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"foodTagCell" forIndexPath:indexPath]; 
     NSDictionary *dic = [arrAmenities objectAtIndex:indexPath.row]; 

     NSMutableDictionary *dic1 = [selectedAmenities objectAtIndex:indexPath.row]; 


      NSURL *url = [NSURL URLWithString:[dic valueForKey:@"ImageURL"]]; 
      [cell.photoImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]]; 


     return cell; 
} 
+0

我應該添加TableCell cellForRowAtIndexPath在我的ViewController和CollectionCell cellForItemAtIndexPath在TableViewCell類的代碼?因爲你可以看到我在我的外部ViewController中有CollectionView cellForItemAtIndexPath方法,當我添加它時,它顯示---- if(myCollection.tag == 0)未解析的標識符'myCollection'的使用---請你解釋一下嗎? –

+0

@AraRose:你可以試試這個myCollection替換爲collectionView。 –