【问题标题】:How to add a collectionView with custom view inside a tableview cell in swift 3?如何在 swift 3 中的 tableview 单元格中添加带有自定义视图的 collectionView?
【发布时间】:2017-07-11 06:24:30
【问题描述】:

我需要在 tableview 单元格的视图中添加一个集合视图。 下图描述了我所需要的。

tableView
- tableViewCell
 - contentView
     -monthView
     -detailedView
         -collectionView
           - collection Cell
               -detailedImageView

我在detailedView(tableview 单元格第二个视图)中添加了collectionview。但是我不知道我是否需要创建collectionView cell.swift文件以及整个过程如何做??

【问题讨论】:

  • 您可以将表格单元格内的collectionview视为普通collectionView!是否需要创建自定义 collectionviewcell 取决于您!我更喜欢创建 collectionview 单元格。
  • @SaurabhPrajapati 您能否提供与此相关的链接或描述。因为我是 iOS 新手。
  • 这个链接可以帮到你:ashfurrow.com/blog/…
  • @Pipiks 谢谢

标签: ios swift uitableview swift3 uicollectionview


【解决方案1】:

首先您需要创建collectionView 单元格文件。下面是一些示例代码,首先为 collectionViews 创建 contentOffset 数组。

var contentOffsets = [Int : CGFloat]()

那么你可以给每一行collectionViews设置tag-s来识别调用了哪个collectionView委托方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: booksListCellId) as! BooksListCell
        cell.collectionView.tag = indexPath.row

        return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let tableCell = cell as! BooksListCell
        tableCell.collectionView.dataSource = self
        tableCell.collectionView.delegate = self
        tableCell.collectionView.reloadData()
        if let offset = contentOffsets[tableCell.collectionView.tag] {
            tableCell.collectionView.setContentOffset(CGPoint(x: offset, y: 0), animated: false)
        }
}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let tableCell = cell as! BooksListCell
        contentOffsets[tableCell.collectionView.tag] = tableCell.collectionView.contentOffset.x
}

并在包含 tableView 的 ViewController 中实现您的 collectionView 数据源和委托方法

这里是tutorial,可能会对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多