【问题标题】:Remove UICollectionViewCell via custom button通过自定义按钮删除 UICollectionViewCell
【发布时间】:2018-11-09 11:40:57
【问题描述】:

这种方法会删除随机单元格,但不是我想要的。我认为问题在于使用 indexPath 初始化按钮,我不知道如何解决。

如果我没记错的话,indexPath.item 表示 UICollectionView 中的一行。那么漏洞在哪里呢?

extension UIButton {

    struct Holder {
        static var _myComputedProperty:IndexPath!
    }
    var indexPath:IndexPath {
        get {
            return Holder._myComputedProperty
        }
        set(newValue) {
            Holder._myComputedProperty = newValue
        }
    }
}

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrRequest.count
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! RequestCell
    cell.delete.indexPath = indexPath
    cell.delete.addTarget(nil, action: #selector(del(sender:)), for: .touchUpInside)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 235)
}

@objc func del(sender: UIButton) {

    arrRequest.remove(at: sender.indexPath.item)

    self.collectionView.performBatchUpdates({
        self.collectionView.deleteItems(at: [sender.indexPath])
    }) { (finished) in
        self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
    } 
}

【问题讨论】:

  • 可以设置一个带有indexPath行的按钮标签。然后在del函数中处理。
  • 您是否尝试过使用断点和/或记录什么?通常只要在调试器 (LLDB) 中打印或使用 p/po 命令就可以看到这些东西
  • @StephenJ 我在 collectionView(_:cellForItemAt:) 中打印了 indexPath ,它似乎工作正常。然后我将单元格 obj 传递给操作函数并添加 guard let indexPath = collectionView.indexPath(for: cell) else { return } 和保护语句始终为 false。我也想用自定义委托重构我的代码到单元格,但是 wtf f() ins't called 。请解释如何正确使用 UICollectionViewCell。
  • 集合和表格视图只需要对动画系统、运行循环和线程有一个全面的了解......我不确定你的错误是什么,除非来自一个来源的数据在预期之前被删除,交叉引用,或者某些东西在将其移动到新线程的回调中......虽然它可能更简单。我相当肯定这与索引+变化时间有关

标签: ios swift cocoa-touch uicollectionview


【解决方案1】:

请更改您的代码,如下所示

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! RequestCell
    cell.delete.tag = index path.row
    cell.delete.addTarget(nil, action: #selector(del(sender:)), for: .touchUpInside)

    return cell
}

你的删除方法如下:

@objc func del(sender: UIButton) {

let indexPath = IndexPath(item: sender.tag, section: 0)

    arrRequest.remove(at: indexPath)

    self.collectionView.performBatchUpdates({
        self.collectionView.deleteItems(at: [indexPath])
    }) { (finished) in
        self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
    } 
}

【讨论】:

  • arrRequest.remove(at: indexPath) 编译错误,arrRequest.remove(at: indexPath.item) 运行时错误
  • 将 .row 替换为 .item,因为它是 UICollectionV。他和桌子混在一起
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 2012-07-16
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多