【问题标题】:Dynamic table view cell height for embedded collection view that is filled after dequeue of table view cell嵌入式集合视图的动态表视图单元格高度,在表视图单元格出列后填充
【发布时间】:2019-03-10 20:12:27
【问题描述】:

我在第 1 行的表格视图单元格中有一个集合视图,该集合视图在单元格已出列后加载了 Firebase 数据库中的内容。因此,在 heightForRowAt: 处的单元格上使用 AutoDimensions 不起作用,因为那时集合视图中没有内容。集合视图锚定到单元格的所有侧面。当集合视图已加载所有数据时,在该特定索引路径更新表格视图单元格高度的最佳方法是什么。谢谢!

在包含表视图的 VC 中,我设置了一个函数,用于从数据库中检索用户列表,该函数在 viewDidLoad() 中调用,并且我设置了协议来重新加载位于tableView 单元格类。

fileprivate func retrieveListOfUsers() {
    print ("fetching users from database")
    let ref = Database.database().reference()
    guard let conversationId = conversation?.conversationId else {return}
    ref.child("conversation_users").child(conversationId).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionaries = snapshot.value as? [String:AnyObject] else {return}
        for (key, _) in dictionaries {
            let userId = key
            Database.fetchUsersWithUID(uid: userId, completion: { (user) in
                self.users.append(user)
                DispatchQueue.main.async { 
                    self.reloadDelegate.reloadCollectionView()
                }
            })
        }
    }, withCancel: nil)
}

这是使单元格出列的代码。我已经删除了这篇文章中第一个单元格的代码,因为它无关紧要并且不想让你阅读不必要的代码。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Code for cell in indexPath.row = 0

    if indexPath.row == 1 {
        let usersCell = tableView.dequeueReusableCell(withIdentifier: usersCellId, for: indexPath) as! UsersTableViewCell
        usersCell.backgroundColor = .clear
        usersCell.selectionStyle = .none
        usersCell.collectionView.delegate = self
        usersCell.collectionView.dataSource = self
        self.reloadDelegate = usersCell
        usersCell.users = users
        return usersCell
    }
}

如果我返回 UITableView.automaticDimension 集合视图不显示。它只显示我是否将函数设置为返回 200 之类的常量。我在 viewDidLoad() 中设置了估计行高度和行高度。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 {
        return UITableView.automaticDimension
    }
    return UITableView.automaticDimension
}

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    为集合视图加载数据源后,您可以像这样更新表中的特定行:

    UIView.performWithoutAnimation {
       tableView.reloadRows(at: [indexPath], with: .none)
    }
    

    对于行高-尝试删除方法实现,如果您已经为estimatedRowHeight设置了一个值就足够了。

    【讨论】:

    • 感谢您的建议,但没有奏效。我把tableView.reload代码放在reloadCollectionView的地方,但是集合还是没有显示出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多