静态TableViewCell

时间:2015-05-30 18:23:59

标签: ios uitableview swift uicollectionview

我在网上看过很多关于在动态表视图单元格中嵌入UICollectionViews的教程,并对集合视图进行子类化以设置委托,但我想知道静态表格视图单元格的过程是否有所不同。

我尝试按照示例here但我无法完全遵循它,因为它似乎过于复杂,几乎没有解释。有人会介意我需要完成的基本步骤才能使我的集合视图工作吗?

到目前为止,这是我的代码:

class FeaturedController: UITableViewController, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    popularCollectionView.dataSource = self
    popularCollectionView.delegate = self
}

//MARK: Popular Events Collection View
    @IBOutlet weak var popularCollectionView: UICollectionView!

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = popularCollectionView.dequeueReusableCellWithReuseIdentifier("popular", forIndexPath: indexPath) as! UICollectionViewCell

        return cell
    }

非常感谢!

2 个答案:

答案 0 :(得分:6)

如果您知道如何将集合视图添加到动态表格视图单元格中,则将其添加到静态表单单元格中会更容易。你根本不需要任何子类(但这样做可能是一个好主意)。在幕后,静态表视图只不过是一个普通的表视图,它得到了托管UITableViewController的支持,可以自动设置Interface Builder中的布局。所以,这是如何做到的:

  1. 从Interface Builder中的对象库中拖动集合视图,并将其放在所需的单元格中。
  2. 确保表视图由表视图控制器托管。
  3. 在单元格中设置约束或布局集合视图。
  4. 将Collection View的dataSource设置为托管的Table View Controller。
  5. UICollectionViewDataSource一致性添加到表视图控制器。
  6. UICollectionViewDataSource中实施collectionView:numberOfItemsInSection:的方法,即collectionView:cellForItemAtIndexPath:UITableViewController
  7. 如果您知道如何使用UITableViewUICollectionView,那么这应该不难理解。

    <强>更新 你的代码看起来应该有效。 所以,你应该检查一下:

    1. 您确实已将Table View Controller的类设置为FeaturedController类。
    2. 您确实已将Interface Builder中的Collection View连接到popularCollectionView
    3. 您已经拥有标识为popular的原型集合视图单元格。虽然如果你还没有这样做,它应该会崩溃。
    4. 在IB中,您已将表视图设置为静态。
    5. 我在这里做了一个小例子

      Interface Builder

      橙色视图是集合视图,绿色视图是标识为myCell的原型集合视图单元格。

      Data Source

      然后我将视图控制器设置为Collection View的数据源。但你也可以在代码中设置它,就像你一样。

      然后我实现了下面的数据源方法。

      @interface ViewController () <UICollectionViewDataSource>
      
      @end
      
      @implementation ViewController
      
      - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
          return 20;
      }
      
      - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
          UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
          return cell;
      }
      
      @end
      

      这就是结果:

      result

答案 1 :(得分:0)

对于TableView 静态单元格

在连接了集合视图的视图控制器中,添加视图确实加载了集合视图的委托和数据源,如果使用情节提要,这会将委托添加到表格视图单元格中,您将其连接错了,因为委托和数据源必须在单元初始化内。

@IBOutlet weak var quotesCollectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    quotesCollectionView.delegate = self
    quotesCollectionView.dataSource = self
}                                                                                           

对于TableView 动态单元格

class QuotesTableViewCell: UITableViewCell {

// MARK: Properties

@IBOutlet weak var quotesCollectionView: UICollectionView!

// MARK: Initialization

override func awakeFromNib() {
    super.awakeFromNib()
    quotesCollectionView.delegate = self
    quotesCollectionView.dataSource = self
   }
}