2017-11-23 8 views
0

tableViewCell (here) 안에 collectionViews를 추가하는 방법에 대한 지침을 따른 후이 두 셀을 전환 할 수 있는지 확인하려고했습니다. 그러나 collectionview (가로로) 아래로 스크롤하려고하면 collectionview 인덱스가 범위를 벗어난다는 오류가 발생합니다. 나는 그 문제에 도움이 될 각각의 콜렉션 뷰에 대한 데이터를 다시로드를 추가 있는지 확인하려고하지만 ... 아래 코드는하지 않습니다indexpath 이동 후 UITableviewCell 내의 UICollectionView

class BuildTravelPackage: UIViewController, UITableViewDataSource,UITableViewDelegate, UICollectionViewDelegate { 

@IBOutlet var optionsTableView: UITableView! 

var items = [[PackageOption]]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(longPressGestureRecognizer:))) 
    self.optionsTableView.addGestureRecognizer(longPressRecognizer) 

    //load data into cells 



} 

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.alpha = 0 
    UIView.animate(withDuration: 1.0) { 
     cell.alpha = 1.0 
    } 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 175 
} 

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool { 
    return false 
} 

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 
    return .none 
} 

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 
    let item = items[sourceIndexPath.row] 
    items.remove(at: sourceIndexPath.row) 
    items.insert(item, at: destinationIndexPath.row) 
    optionsTableView.isEditing = !optionsTableView.isEditing 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = optionsTableView.dequeueReusableCell(withIdentifier: "optionCell", for: indexPath) as! PackageOptionTableViewCell 
    cell.collectionView.delegate = self 
    cell.collectionView.dataSource = self 
    cell.collectionView.tag = indexPath.row 
    cell.collectionView.reloadData() 
    return cell 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    print("Column:",indexPath.row) 
    print("Row:",collectionView.tag) 
    //highlght selected cell and load a new cell with infomation? Or load a uiview with date selection 
} 

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) { 

    if longPressGestureRecognizer.state == UIGestureRecognizerState.began { 

     let touchPoint = longPressGestureRecognizer.location(in: self.view) 
     if let indexPath = optionsTableView.indexPathForRow(at: touchPoint) { 
      optionsTableView.isEditing = !optionsTableView.isEditing 

     } 
    } 
} 
} 


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

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleOptionCell", for: indexPath) as! OptionCollectionViewCell 
    cell.image.image = items[collectionView.tag][indexPath.row].imageFile 
    cell.label.text = items[collectionView.tag][indexPath.row].name 
    return cell 
} 

} 

답변

1

나는 문제가 교체 할 객체가있는 경우 여기에 생각

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 
let item = items[sourceIndexPath.row] 
items.remove(at: sourceIndexPath.row) 
items.insert(item, at: destinationIndexPath.row) 
optionsTableView.isEditing = !optionsTableView.isEditing 
} 

는 통찰력을위한

swap(& items[sourceIndexPath.row], & items[destinationIndexPath.row]) 
+0

감사에 의해 코드를 교체해보십시오! 나는이 기회를 포기하고 다음 주에 내 컴퓨터에 액세스 할 때 어떤 일이 일어나는지 계속 알려줄 것입니다. –

+0

문제가 지속되는 것 같습니다. 왜 그런지 모르겠다. –

+1

항목을 이동 한 후 tableview를 다시로드 해보십시오. 즉, func tableView (_ tableView : UITableView, sourceRef, sourceIndexPath : IndexPath, destinationIndexPath : IndexPath) {} – Vikky