2016-08-27 3 views
1

Im пытается получить VC, который соответствует ячейке таблицы просмотра для загрузки при нажатии. Я пытаюсь установить indexPath как selectedIndexPath и настроить segue в представлении коллекции, а затем использовать его в tableView, но он бросает ошибку. Вот код.загрузка UITableViewController из выбранного индексного пути в UICollectionViewController

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», причина: «запрос на ошибку при неверном пути указателя – 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 с вашей точки зрения выбора в настоящее время смешались, код размещен выше, должны помочь вам отлаживать это.

Кроме того, я полагаю, вы можете сделать свой предполагаемый результат по:

1) Сохранение selectedIndexPath в didSelectCellAtIndexPath: метод

2) Считывание selectedIndexPath в prepareForSegue вместо выведения indexPath из Segue отправитель