ios - 移动索引路径后 UITableviewCell 内的 UICollectionView

标签 ios arrays uitableview swift3 uicollectionview

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

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

关于ios - 移动索引路径后 UITableviewCell 内的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47462850/

相关文章:

iphone - iOS:如何访问 `UIKeyboard`?

ios - 使用 -fno-objc-arc 仍然给我 Convert to ARC 错误

iOS:强制取消 NSOperationQueue 中的操作

ios - 在带有标签的 TableView 中添加自定义按钮

c++ - 更优雅的方法来检查 C++ 数组中的重复项?

javascript - 如何测试数组项的文本是否包含子字符串?

c++ - 使用除法技术求数组的最大公约数

iOS 自定义 UItableView 标题

ios - TableViewController 的pushViewController 方法中的navigationController = null (popover)

ios - 为标识符注册的无效 Nib