首页 > 解决方案 > 如何控制单元格重定义

问题描述

我有三个部分,第一个存储用户信息,第二个包含添加帖子的字段,第三个是帖子。滚动时,我开始加载添加帖子的部分和帖子部分中的用户信息部分

这是我的代码 UIViewController 扩展 UITableViewDataSource :

    func numberOfSections(in tableView: UITableView) -> Int {
        presenter?.numberOfSections() ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let section = UserPageTypeSection(rawValue: section) else { return 0 }
        return presenter?.numberOfRowsInSection(section: section) ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let section = UserPageTypeSection(rawValue: indexPath.section),
              let userInfo = presenter?.cellForRowAt(section: section, indexRow: indexPath.row)
              else { return UITableViewCell() }
        switch userInfo.type {
        case .userInfo:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: ProfileInfoTableViewCell.reuseIdentifier, for: indexPath) as? ProfileInfoTableViewCell else { return UITableViewCell() }
            cell.userInformation(userData: userInfo.data?[indexPath.row] as? UserProfile)
            return cell
            
        case .userAddPost:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: ProfileInfoTableViewCell.reuseIdentifier, for: indexPath) as? ProfileInfoTableViewCell else { return UITableViewCell() }
            cell.addPostUser()
            return cell
        case .userPost:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: ProfileInfoTableViewCell.reuseIdentifier, for: indexPath) as? ProfileInfoTableViewCell else { return UITableViewCell() }
            cell.post(text: "POST")
            return cell
        default:
            return UITableViewCell()
        } 

标签: iosswiftuitableview

解决方案


推荐阅读