2016-08-27 1 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 

    } 
} 

}

하고 collectionView 경우 다음

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

아, 당신은 확인되지? .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가 섞여있는 경우 위의 코드는 디버깅에 도움이됩니다. SEGUE에서 indexPath을 방법

2) prepareForSegue에서 selectedIndexPath을 읽는 대신 파생 다음 didSelectCellAtIndexPath에 selectedIndexPath 저장)

1 : 그 너머

, 난 당신에 의해 의도 한 결과를 얻을 수 있다고 생각 발신자

관련 문제