在 Tableview 中的数组转换中不显示

问题描述 投票:0回答:1

在应用程序开始时,我可以成功看到todo数组。但是,将待办事项移至“进行中”或“完成”状态后,当我再次返回“待办事项”状态时,todoGoals 数组在屏幕上不可见。只有我最后点击的部分可见。

主页视图模型:

import Foundation

protocol HomeViewModelInterface {
    var view: HomeScreenInterface? { get set }
    func viewDidLoad()
    func viewToDoGoals()
    func viewInProgressGoals()
    func viewDoneGoals()
}

final class HomeViewModel {
    weak var view: HomeScreenInterface?
    var progressSections: [String] = ["To Do","In Progress","Done"]
    
    lazy var toDoGoals: [ToDoModel] = [
        ToDoModel(title: "ToDo 1"),
        ToDoModel(title: "Todo 2"),
        ToDoModel(title: "ToDo 3")
    ]
    
    lazy var inProgressGoals: [ToDoModel] = [
        ToDoModel(title: "Progress 1"),
        ToDoModel(title: "Progress 2"),
        ToDoModel(title: "Progress 3")
    ]
    
    lazy var done: [ToDoModel] = [
        ToDoModel(title: "Done 1"),
        ToDoModel(title: "Done 2"),
        ToDoModel(title: "Done 3")
    ]
}

extension HomeViewModel: HomeViewModelInterface {
    
    func viewDidLoad() {
        view?.configureVC()
        view?.configureCollectionView()
        view?.configureTableView()
    }
    
    func viewToDoGoals() {
        self.view?.updateTableView(with: self.toDoGoals)
        print("View todo")
    }
    
    func viewInProgressGoals() {
        self.view?.updateTableView(with: self.inProgressGoals)
        print("View progress")
    }
    
    func viewDoneGoals() {
        self.view?.updateTableView(with: self.done)
        print("View Done.")
    }
}

主屏幕: 我更新tableview的协议函数在这里。

protocol HomeScreenInterface: AnyObject {
    func configureVC()
    func configureCollectionView()
    func configureTableView()
    func updateTableView(with goals: [ToDoModel])
}

final class HomeScreen: UIViewController {
        override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.view = self
        viewModel.viewDidLoad()
    }

extension HomeScreen: HomeScreenInterface {

    func configureVC() {
        title = "To Do ✅"
        view.backgroundColor = .systemBackground
    }
    // Update TableView Array
    func updateTableView(with goals: [ToDoModel]) {
        viewModel.toDoGoals = goals
        DispatchQueue.main.async {
            self.goalsTableView.reloadData()
        }
    }
  }
}

TableView 委托和数据源方法:

extension HomeScreen: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.toDoGoals.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: GoalsCells.reuseIdentifier, for: indexPath) as? GoalsCells else {
            return UITableViewCell()
        }
        cell.delegate = self
        
        let goals = viewModel.toDoGoals[indexPath.row]
        cell.setGoalCell(goal: goals.title, checked: goals.isComplete)
        return cell
    }
}

CollectionView 委托和数据源方法:

extension HomeScreen: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProgressCells.reuseIdentifier, for: indexPath) as? ProgressCells else {
            return UICollectionViewCell()
        }
        
        cell.setCell(progress: viewModel.progressSections[indexPath.item])
        return cell
    }
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        switch indexPath.item {
        case 0: // To Do
            self.viewModel.viewToDoGoals()
        case 1: // In Progress
            self.viewModel.viewInProgressGoals()
        case 2: // Done
            self.viewModel.viewDoneGoals()
        default:
            break
        }
    }

}

型号:

import Foundation

struct ToDoModel {
    let title: String
    let isComplete: Bool
    
    init(title: String, isComplete: Bool = false) {
        self.title = title
        self.isComplete = isComplete
    }
    
    func completeToggled() -> ToDoModel {
        return ToDoModel(title: title, isComplete: !isComplete)
    }
}

图片:

arrays swift uitableview uicollectionview uikit
1个回答
0
投票

这是因为按照您当前的逻辑,每当用户点击“正在进行”或“完成”单元格时,该点击事件都会覆盖

toDoGoals
属性。

extension HomeScreen: HomeScreenInterface {
    //...
    // Update TableView Array
    func updateTableView(with goals: [ToDoModel]) {
        viewModel.toDoGoals = goals // Here: toDoGoals is updated every time user taps on other cells.
        DispatchQueue.main.async {
            self.goalsTableView.reloadData()
        }
    }
  }
}

因此,

toDoGoals
数组中的原始元素通过点击其他单元格被消除。我认为你可以通过两种方式处理这个问题。

  1. 您可以创建另一个数组变量来存储要在表格视图上显示的项目。
  2. 或者您可以利用
    enum
    来指示您要显示的任务类型,并让表格视图显示相关的
    [ToDoModel]
    属性。

希望这可以帮助您调试这个问题!

© www.soinside.com 2019 - 2024. All rights reserved.