从字典数组中填充表节和行 [英] Populate Table Section and Rows from Array of Dictionaries

查看:75
本文介绍了从字典数组中填充表节和行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要从存储模型数据的类中填充的表.首先,我的数据存储为字典数组,如下所示:

I have a table that I'm trying to populate from a class that stores the model data. Firstly, my data is stored as an array of dictionaries as follows:

var pupilSubjects = ["Tom":["English", "Geography", "History"], "Dick":["English", "Geography", "Physical Education", "Biology"], "Harry": ["English", "Geography", "Physical Education", "Biology"]]

在viewDidLoad的TableViewController中,将信息存储在字典数组中,然后将每个字典添加到我称为TableText的类中,如下所示:

In my TableViewController in viewDidLoad I take the information in my array of dictionaries and add each dictionary to a class that I called TableText like so:

for dict in pupilSubjects {

        let key = dict.0
        let values = dict.1


       tableTexts.append(TableText(name: key, subject: values))
    }

我的变量tableTexts如下存储在我的TableViewController中:

my variable tableTexts is stored in my TableViewController as follows:

var tableTexts = [TableText]()

我的TableText类如下:

My TableText class is as follows:

import UIKit

class TableText: NSObject {

var name: String
var subject: [String]


init(name: String, subject: [String]) {
    self.name = name
    self.subject = subject

  }

}

我有一个自定义的TableViewCell,我将其称为myTableViewCell,在其中按如下方式设置tableText数据:

I have a custom TableViewCell which I call myTableViewCell where I set tableText data as follows:

   var tableText: TableText? {
    didSet {
        let myInputText = myTextView.text
        tableText!.subject.append(myInputText)
        //  stepNumber.text = msStep!.step

    }
}

我正在努力在TableViewController中实现cellForRowAtIndexPath,以显示存储在var tableTexts = TableText中的节和行详细信息.我可以像这样从我的部分标题中获取密钥:

I am struggling to implement cellForRowAtIndexPath in my TableViewController to display my section and row details that I have stored in my var tableTexts = TableText. I can get the keys from my section title like so:

let myKey = sectionTitles[indexPath.section]

但是我不确定如何使用每个键来返回存储在tableTexts中的字符串数组.任何帮助/指针将不胜感激.

but I'm unsure how to use each key to return the array of strings stored in tableTexts. Any help / pointers would be greatly appreciated.

推荐答案

根据您的规范,相关的表视图委托/数据源方法为

According your specification the relevant table view delegate / data source methods are

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  let pupil = tableTexts[section]
  return pupil.name
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  return tableTexts.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  let pupil = tableTexts[section]
  return pupil.subject.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

  let pupil = tableTexts[indexPath.section]
  cell.textLabel!.text = pupil.subject[indexPath.row]
  return cell
}

这篇关于从字典数组中填充表节和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆