swift - 从 UICollectionViewCell 中点击按钮时如何引用 UITableViewCell?

标签 swift uitableview uicollectionview

Tapping on UICollectionViewCell Button

我在 UIViewController 中有一个 UICollectionView 和一个 UITableView。选择 UITableView 中的行会将项目添加到 UICollectionView,以相同的方式取消选择 UITableView 中的行会从 UICollectionView 中删除项目。这是通过将 UITableView 对象添加/删除到其类的数组中来完成的。如果表格已被选中(用户已添加到数组中),该单元格的 accessoryType 将更改为复选标记,当用户被删除时,它会变回无。

var selectedUsers = [User]()

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? NewGroupUserTableViewCell {
            if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
                tableView.cellForRow(at: indexPath)?.accessoryType = .none
                if let user = cell.user {
                    self.selectedUsers.removeAll{$0.uid == user.uid}
                }
            } else {
                tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
                    if let user = cell.user {
                        self.selectedUsers.append(user)
                    }
            }
            self.collectionView.reloadData()
        }
    }

在 UICollectionViewCell 类中,我有一个协议(protocol)可以在点击删除按钮时调用 UIViewController 中的函数。每当点击删除按钮时,用户将从数组中删除,因此从 UICollectionView 中删除该项目。我遇到的问题是在点击 UICollectionViewCell 中的删除按钮时更新 UITableViewCell 中的 accessoryType。我不知道如何从 UICollectionView 引用特定的 UITableViewCell。

我能想出的最好方法是为 UITableViewCells 使用 for 循环查找具有匹配 ID 的对象,然后在找到匹配项时更新 accessoryType。这并不总是有效。

这是我的 UICollectionViewCell 类委托(delegate)中的函数。

func didTapDelete(user: User) {
    let selectedUser = selectedUsers.first(where: { $0.uid == user.uid })
    for section in 0 ..< 2 {
        let rowCount = tableView.numberOfRows(inSection: section)

        for row in 0 ..< rowCount {
            let cell = tableView.cellForRow(at: NSIndexPath(row: row, section: section) as IndexPath) as! NewGroupUserTableViewCell
            if cell.user.uid == selectedUser?.uid {
                cell.accessoryType = .none
            }
        }
    }

    selectedUsers.removeAll{$0.uid == user.uid}
    self.collectionView.reloadData()
}

点击 UICollectionViewCell 中的按钮时如何引用 UITableViewCell?

最佳答案

首先你的方法可以简化,不要试图在 Collection View 委托(delegate)中改变tableviewcell的属性:

在您的cellForRowAt方法中执行以下操作,将选择样式更新移动到此方法:

func tableView(_ tableView: UITableView,
               cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "NewGroupUserTableViewCell",
                                             for: indexPath) as! NewGroupUserTableViewCell
    let user = users[indexPath.row]
    cell.textLabel?.text = user.uid // Whatever your want to show here, Did this for sample
    if itemsSelected.contains(user) {
        cell.accessoryType = .checkmark
    } else {
        cell.accessoryType = .none
    }
    return cell
}

UICollectionViewCell 委托(delegate)方法中,执行以下操作: 在这里,我再添加一个参数以将单元格传回。

func didTapOnDelete(_ cell: ItemCollectionViewCell, item: User) {
    guard let indexPath = collectionView.indexPath(for: cell) else { return }
    if let index = itemsSelected.firstIndex(of: item) {
        itemsSelected.remove(at: index)
    } else {
        itemsSelected.append(item)
    }
    collectionView.deleteItems(at: [indexPath])
    tableView.reloadData()
}

您的didSelectRowAt 将如下所示:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let userToAdd = users[indexPath.row]
    if let index = itemsSelected.firstIndex(of: userToAdd) {
        itemsSelected.remove(at: index)
    } else {
        itemsSelected.append(userToAdd)
    }
    collectionView.reloadData()
    tableView.reloadRows(at: [indexPath], with: .automatic)
}

注意:为了便于比较,我将 User 类设为 Equatable,如下所示:

extension User: Equatable {
   static func == (lhs: User, rhs: User) -> Bool {
    return lhs.uid == rhs.uid
   }
}

还假设相同的项目不能重复添加到 collectionView。

希望对您有所帮助!

关于swift - 从 UICollectionViewCell 中点击按钮时如何引用 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58060695/

相关文章:

ios - UIScrollView 相对于子 ViewController 的偏移量

ios - 自定义自定义 Url 方案在 iOS 9 中不起作用

iOS滚动到 View 文本字段之外

ios - 使用 reloadData 时如何执行单元格选择动画?

iphone - collectionView dequeueReusableCellWithIdentifier 出错

node.js - 从 Node 到 iOS 应用程序的 HLS

ios - 如何在整个应用程序上按返回键快速隐藏键盘?

iphone - 如何创建类似于 iOS 联系人的 "add new address"功能(iPad 和 iPhone)

ios - 无法显示我的自定义 UITableViewCell

ios - 使用 Swift 3 单击 collectionView 单元格内的项目时如何导航另一个 View Controller