从UICollectionViewController中的选定索引路径加载UITableViewController

时间:2016-08-27 19:34:58

标签: ios swift xcode

我试图获取与表视图的单元格对应的VC以在点击时加载。我正在尝试将indexPath设置为selectedIndexPath并在集合视图中设置segue,然后在tableView中使用它,但它会抛出错误。这是代码。

import UIKit

private let reuseIdentifier = "profileCell"


class CompanyCollectionViewController: UICollectionViewController {

//MARK: - View Did Load

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

}


// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return stuff.company.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ProfileCollectionViewCell

    cell.profileImageView.image = UIImage(named: stuff.company[indexPath.row]["image"]!)

    return cell
}

//MARK: - Prepare For Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "profileSegue" {
        let profileVC = segue.destinationViewController as!
            MyProfileTableViewController
        let cell = sender as! UICollectionViewCell
        let indexPath = collectionView?.indexPathForCell(cell)

        profileVC.selectedIndexPath = indexPath

    }
}

}

然后

import UIKit

class MyProfileTableViewController: UITableViewController {

//MARK: - View Did Load

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





//MARK: - Table View Data Source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let indexPath = selectedIndexPath {

        let row = stuff.company[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("myProfileCell", forIndexPath: indexPath) as! MyProfileTableViewCell
            cell.heroImageView.image = UIImage(named: row["image"]!)
            title = row["name"]
            cell.nameLabel.text = row["name"]
            cell.statusLabel.text = row["description"]
            return cell

    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("myProfileCell", forIndexPath: indexPath) as! MyProfileTableViewCell
        cell.heroImageView.image = UIImage(named: stuff.profile["profile image"]!)
        title = stuff.profile["name"]
        cell.nameLabel.text = stuff.profile["name"]
        cell.statusLabel.text = stuff.profile["status"]
        return cell
    }
}

//MARK: - Variables

var selectedIndexPath: NSIndexPath?

}

1 个答案:

答案 0 :(得分:0)

啊,我看,你没有检查你的collectionView?.indexPathForCell是否返回了有效的结果。

   if let cell = sender as? UICollectionViewCell {
       // Cell is valid
       if let indexPath = collectionView?.indexPathForCell(cell) {
           // indexPath is valid
       } else {
           // indexPath is invalid
       }
   } else {
       // cell is invalid
   }

我怀疑你的表中的indexPath和你的选择视图中的indexPath是混淆的,上面发布的代码可以帮助你调试它。

除此之外,我相信你可以通过以下方式完成预期的结果:

1)在didSelectCellAtIndexPath:方法中保存selectedIndexPath

2)读取prepareForSegue中的selectedIndexPath而不是从segue发送者派生indexPath

相关问题