2016-08-27 6 views
1

タップ時にロードするテーブルビューのセルに対応するVCを取得しようとしています。 indexPathselectedIndexPathとして設定しようとしていて、コレクションビューでsegueを設定してからtableViewで使用しますが、エラーが発生しています。ここにコードがあります。私が見UICollectionViewController内の選択したインデックスパスからUITableViewControllerをロードします。

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

エラーは何ですか? – Lytic

+0

***キャッチされていない例外 'NSInternalInconsistencyException'のためにアプリケーションを終了する、理由: '無効なインデックスパスでrectを要求する – Androoo

答えて

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が混在していると思われる場合は、上記のコードをデバッグすると役立ちます。その向こう

、私はあなたがして、意図した結果を達成できると信じて:didSelectCellAtIndexPathであなたのselectedIndexPathを保存)

1:方法

2)prepareForSegueにselectedIndexPathを読む代わりにセグエからindexPathを導出します送信者

関連する問題