ios - TableView 中的 Collectionview - 如何选择项目?

标签 ios swift uitableview swift3

我在 TableView 中创建了一个 CollectionView,用于垂直和水平滚动以及可自定义的单元格。到目前为止这有效。问题是:我无法选择 CollectionView 的项目。我认为问题可能与代表网点有关,但我找不到解决方案。

我是 Swift 的新手,所以我可能忽略了一些明显的东西。

我的TableViewController:

import UIKit

class HomeVTwoTableViewController: UITableViewController {

    var headers = ["Live", "Friends", "Last commented"]

    @IBAction func cancelBtnPressed(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return headers[section]
    }

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){

        view.tintColor = UIColor.black
        let header = view as! UITableViewHeaderFooterView
        if section == 0 {
            header.textLabel?.textColor = UIColor.black
            view.tintColor = UIColor.white
        }
        else {
            view.tintColor = UIColor.groupTableViewBackground
        }
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return headers.count
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellBig", for: indexPath) as! HomeVTwoTableViewCell

            return cell
        }
        else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellSmall", for: indexPath) as! HomeVTwoTableViewCellSmall

            return cell
        }
        else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellSmall", for: indexPath) as! HomeVTwoTableViewCellSmall

            return cell
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if indexPath.section == 0 {
            return 225.0
        }
        else {
            return 120.0
        }
    }
}

我的 TableViewCellCollectionView:

import UIKit

class HomeVTwoTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var collectionView: UICollectionView!

    fileprivate var images = [UIImage]()
    {
        didSet
        {
            self.collectionView.reloadData()
        }
    }

    func setup(for images: [UIImage])
    {
        self.images = images
    }

    override func layoutSubviews() {
        collectionView.dataSource = self
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return communityName.count
    }

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

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("didSelect")
        selectedCommunity = communityId[indexPath.row]
        let home = HomeViewController()
        home.showCommunityDetail()
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        guard let cell =   collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellBig", for: indexPath) as? HomeVTwoCollectionViewCell else
        {
            fatalError("Cell has wrong type")
        }

       //cell.imageView.image = image
       cell.titleLbl.text = communityName[indexPath.row]
       cell.imageView.downloadedFrom(link :"deleted because privat")

       return cell
    }
}

我的CollectionViewCell:

import UIKit

class HomeVTwoCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var titleLbl: UILabel!
}

最佳答案

你需要像这样设置delegate和dataSource

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView.dataSource = self
    collectionView.delegate   = self
}

关于ios - TableView 中的 Collectionview - 如何选择项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44454561/

相关文章:

ios - 不同设备上的 BLE 外设名称不同

swift - 将键更改为 true 然后快速显示 View

swift - 有什么办法可以永久退出自动调整大小?

ios - 在更新 TableView 时,如何使用搜索栏对 NSFetchedResults 数据进行排序,而不会在 fetchedRC.performFetch() 上崩溃?更新

ios - 当键盘切换到从右到左的语言时更改文本对齐方式?

ios - RestKit 获取请求 block (删除孤立对象)

arrays - 编码自定义对象的 nsarray 时无法使用 Hashable

swift - 如何使用自定义模型类按部分字母顺序对 tableView 中的数据进行排序?

ios - UITableview Cell 动画只播放一次

iOS - 将 cocoapods 添加到项目后 git merge 冲突