当水平滚动集合视图时,如何确定选择哪个tableView单元格

时间:2018-05-19 23:52:26

标签: ios swift swift3 tableview collectionview

我在TableViewCell中有collectionView。 TableView有1个部分和4行。

我想弄清楚当collectionView水平滚动时,选择了哪一行tableviewcell。

我试着把tableView行放在" var nowRow"变量如下面的代码。但它不起作用。

如何找出选择哪一行tableviewcell?

class Home2ViewController: UIViewController, UITableViewDataSource,  UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

    var posts = [Post]()
    var nowRow = Int()

    override func viewDidLoad() {
        // get posts elements here 
        //posts.append(element).......
    }

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

            return posts.count // 4counts 

    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       // let cell = DetailMutiTableViewCell()
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailMutiTableViewCell", for: indexPath) as! DetailMutiTableViewCell

        nowRow = indexPath.row

        cell.post = posts[nowRow]
        cell.collectionView1.delegate = self
        cell.collectionView1.dataSource = self

        cell.collectionView1.reloadData()
        return cell
    }



    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return posts[nowRow].imageUrls.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MultiImagesCollectionViewCell", for: indexPath) as! MultiImagesCollectionViewCell

        let photoUrlString = posts[nowRow].imageUrls[indexPath.item] // ←error occured
            let photoUrl = URL(string: photoUrlString)
            cell.imageView1.sd_setImage(with: photoUrl)


        return cell
    }
}

editeditedit

1 个答案:

答案 0 :(得分:0)

您无疑发现,与表行中的collectionViews交互不会触发tableView方法tableView(_:didSelectRowAt:),而tableView.indexPathForSelectedRownil因为没有选择任何行。您需要一种从collectionView映射到包含它的indexPath.row的方法。此信息在tableViewCell设置时知道。

Home2ViewController添加字典,将UICollectionView映射到行Int

var collectionViewRow = [UICollectionView : Int]()

tableView(_:cellForRowAt:)中添加您知道rowcollectionView的条目:

collectionViewRow[cell.collectionView1] = indexPath.row

然后,只要您有collectionView,就可以查找其行:

let row = collectionViewRow[collectionView]!

这里是整个代码:

class Home2ViewController: UIViewController, UITableViewDataSource,  UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

    var posts = [Post]()
    var collectionViewRow = [UICollectionView : Int]()

    override func viewDidLoad() {
        // get posts elements here
        //posts.append(element).......
    }

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

        return posts.count // 4counts
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // let cell = DetailMutiTableViewCell()
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailMutiTableViewCell", for: indexPath) as! DetailMutiTableViewCell

        collectionViewRow[cell.collectionView1] = indexPath.row

        cell.post = posts[indexPath.row]
        cell.collectionView1.delegate = self
        cell.collectionView1.dataSource = self

        cell.collectionView1.reloadData()
        return cell
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let row = collectionViewRow[collectionView]!
        return posts[row].imageUrls.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MultiImagesCollectionViewCell", for: indexPath) as! MultiImagesCollectionViewCell

        let row = collectionViewRow[collectionView]!
        let photoUrlString = posts[row].imageUrls[indexPath.item] // ←error occured
        let photoUrl = URL(string: photoUrlString)
        cell.imageView1.sd_setImage(with: photoUrl)

        return cell
    }
}

注意:

  1. 字典查找的强制解包不会失败,因为collectionView将在那里。如果你想用if let打开它,你需要决定当你没有排好时该做什么(这不应该发生)。
  2. 如果您的表允许编辑(用户可以重新排列行的顺序或删除行),此方法可能会失败。如果您不支持编辑,那么这将正常工作。要支持编辑,您可以将字典更改为[UICollectionView : UITableViewCell]。然后,给定UICollectionView,您可以查找其cell,然后拨打let indexPath = tableView.indexPath(for: cell)以获取indexPath,然后您的行只有indexPath.row。< / LI>
相关问题