Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift 3中的tableview单元格内添加具有自定义视图的collectionView?_Ios_Swift_Uitableview_Swift3_Uicollectionview - Fatal编程技术网

Ios 如何在swift 3中的tableview单元格内添加具有自定义视图的collectionView?

Ios 如何在swift 3中的tableview单元格内添加具有自定义视图的collectionView?,ios,swift,uitableview,swift3,uicollectionview,Ios,Swift,Uitableview,Swift3,Uicollectionview,我需要在tableview单元格中的视图中添加集合视图。 下图描述的正是我所需要的 tableView - tableViewCell - contentView -monthView -detailedView -collectionView - collection Cell -detailedImageView 我在detailedView(tableview单元格第二视图)中添加了collect

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

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

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

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

var contentOffsets = [Int : CGFloat]()
然后可以为每行collectionView设置标记,以识别调用了哪个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数据源和委托方法


这可能对您有帮助。

您可以将表单元中的CopeVIEW视为正常的CopyVIEW视图!这取决于您是否需要创建自定义collectionviewcell!我更喜欢创建collectionview单元格。@SaurabhPrajapati您能提供一个与此相关的链接或描述吗。因为我是iOS新手。这个链接可以帮助你:@Pipiks谢谢