首页 > 解决方案 > 从 UICollectionViewCell 中点击按钮时,如何引用 UITableViewCell?

问题描述

点击 UICollectionViewCell 按钮

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

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 类中,我有一个协议可以在点击删除按钮时调用 UIViewController 中的函数。每当点击删除按钮时,用户就会从数组中删除,因此会从 UICollectionView 中删除该项目。当点击 UICollectionViewCell 中的删除按钮时,我遇到的问题是更新 UITableViewCell 中的附件类型。我不知道如何从 UICollectionView 中引用特定的 UITableViewCell。

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

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

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?

标签: swiftuitableviewuicollectionview

解决方案


首先,您的方法可以简化,不要尝试在集合视图委托中更改 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 Method中执行以下操作: 这里我添加了一个参数以将单元格传回。

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。

希望能帮助到你!.


推荐阅读