移动索引路径后 UITableviewCell 内的 UICollectionView

UICollectionView inside UITableviewCell after moving the indexpath

遵循有关如何在 tableViewCell 中添加 collectionView 的指南后 (here) 然后我尝试查看是否可以切换这两个单元格。 但是,当我尝试向下滚动集合视图(水平)时,出现集合视图索引超出范围的错误。我试图查看为每个集合视图添加重新加载数据是否有助于解决该问题,但它没有...代码如下:

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
}

}

我认为问题出在您替换对象时

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])