【问题标题】:MVVM proper ViewModel format creation to display data in tableview swift iOSMVVM正确的ViewModel格式创建以在tableview swift iOS中显示数据
【发布时间】:2021-11-29 17:49:21
【问题描述】:

我想创建一个具有适当格式的视图模型,可用于在 tableview 中显示数据。

预期输出:

Expected output screenshot image

我尝试创建如下视图模型,但我无法以正确的格式创建它并且它无法正常工作。

struct MyViewModel {
   var headerlist : [String]
   var listItem : [ListData] {
       get {
           return [ListData(title: "Check Detailed Info", type: .INFORMATION),ListData(title: "Check Document", type: .DOCUMENTS), ListData(title: "Check Policy", type: .DOCUMENTS)]
       }
   }
}
struct ListData {
   var title: String
   var type: HeaderType
}
enum HeaderType {
   case INFORMATION
   case DOCUMENTS
}

如何创建一个可以在下面的 tableview 委托方法中使用的视图模型。

    let viewModel =  MyViewModel()
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.headerlist.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionitem = viewModel.headerlist[indexpath.section]
        return sectionItem.listItem.count
    }
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        ///Will be creating a headerview and title label outlet in it.
        headerView.titleLabel.text = viewModel?.headerlist[section]
        return headerView
    }

我刚刚找到了一些链接,希望得到适当的可理解答案

UITableView with MVVM using Swift

Populate data onto UITableView with MVVM

MVVM in TableView Cell

【问题讨论】:

  • 这与视图模型无关,而是模型的设计,您应该有一个包含标题属性和 ListItem 数组的类型,然后您的视图模型可以保存该类型的数组

标签: ios swift xcode mvvm viewmodel


【解决方案1】:

您有两个部分,因此您的模型中需要两个数组。

我建议你使用这样的东西:

struct SectionData {
    let type: HeaderType
    let items: [String]
}

enum HeaderType {
   case information = "INFORMATION"
   case documents = "DOCUMENTS"
}

struct MyViewModel {

    var sectionData : [SectionData] {
        get {
            return [
                SectionData(type: .information, items: ["Check Detailed Info"]), 
                SectionData(type: .documents, items:["Check Document","Check Policy"])
            ]
        }
    }
}

然后你可以在你的表格视图中使用它

let viewModel =  MyViewModel()
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.sectionData.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.sectionData[section].count
    }
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        ///Will be creating a headerview and title label outlet in it.
        headerView.titleLabel.text = viewModel.sectionData[section].type.rawValue
        return headerView
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: indexPath) -> UITableViewCell? {
        let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = viewModel.sectionData[indexPath.section].items[indexPath.row]
        return cell
    }

请注意,这不是 MVVM 方法,但也许您已出于问题的目的简化了代码。如果要使用静态数据,在视图模型init 中创建数据数组会比使用计算属性更有效。

【讨论】:

  • 感谢根据您的回答进行一些修改可以达到预期的格式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2019-10-08
相关资源
最近更新 更多