【问题标题】:Swift - Populating TableViews with multiple sections from multidimensional arraySwift - 使用多维数组中的多个部分填充 TableView
【发布时间】:2016-07-06 22:02:29
【问题描述】:

我是 Swift 的初学者,并且至少了解了如何填充 UITableView 的基本知识。我现在被困在用字典提供的数据填充多个部分。

我有一个将对象分配给类别的多维字典:

var categoryDict:[String:[AnyObject]] = ["Category A": ["Object 1","Object 2"], "Category B":["Object 3"]]

现在我想填充我的 TableView,使其显示如下内容:

  • A 类

    • 对象 1
    • 对象 2
  • B 类

    • 对象 3

到目前为止,我能够创建一个类别数组来返回部分的数量,并计算存储在字典中的数组以获取每个特定类别中的行数。现在我完全坚持用部分和行填充 TableView。我怎样才能做到这一点?非常感谢!

到目前为止我的代码:

var categoryList:[String] = [String]()
var categoryDict:[String:[AnyObject]] = ["Category A": ["Object 1","Object 2"], "Category B":["Object 3"]]


func getLists() {
    categoryList = Array(categoryDict.keys)
    categoryList.sortInPlace(before)
}

// Sort Array
func before(value1: String, value2: String) -> Bool {
    return value1 < value2;
}

func getNumberOfEntrysInSection (Section: Int) -> Int {

    let category:String = categoryList[Section]    //Get Category for Index in CategoryList

    let value:[AnyObject] = categoryDict[category]! //Get Value for this specific Category

    let number = value.count

    return number
}


// MARK: - Table view data source

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


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return getNumberOfEntrysInSection(section)
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return categoryList[section]
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("BufferCell", forIndexPath: indexPath)

    ???
    return cell
}

【问题讨论】:

  • 你设置了代理和数据源吗?
  • 我认为你在正确的轨道上。兄弟你有什么问题?
  • 我认为委托和数据源设置正确 - 我可以用一个部分填充一个表,但多个部分是问题:我找不到说“让第 0 部分的标题是 categoryList 中特定索引处的字符串,然后使用 categoryDict 中为该特定类别提供的值填充该部分中的单元格”。你懂我的意思吗?我读过的所有教程似乎都以不同的方式做到这一点(我不太明白),但是当我从类别字典中提供数据时,必须有一种方法来输入数据?跨度>

标签: ios swift uitableview


【解决方案1】:

首先:字典是存储表视图内容的糟糕选择。字典本质上是无序的,因此您必须不断对键进行排序。如果您的数据超出少量项目,排序过程将需要相当长的时间。

如果你还是要使用字典,你应该重构你的代码,这样你就可以保留排序的键并反复使用它们,除非字典发生变化。我会这样做,以便您向字典添加一个 setter 方法并始终使用该 setter 来更改字典。 setter 方法将重新生成已排序的键。这样,您只需在字典更改时对键进行排序。 (但最好完全摆脱字典。)

我建议创建一个 Section 对象,其中包含一个 sectionTitle String 和一个 sectionEntries 数组。然后使表格视图数据成为 Section 对象的数组。

但是,既然您有字典,我将向您展示如何使该代码工作。

您需要有关 cellForRowAtIndexPath 方法的帮助。你快到了。您只需要使用 indexPath 部分和行在数据结构中获取适当的条目。像这样的:

override func tableView(tableView: UITableView, 
  cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{

    let cell = tableView.dequeueReusableCellWithIdentifier("BufferCell",
     forIndexPath: indexPath)
    let section = indexPath.section 
    let row = indexPath.row

    let categoryKey:String = categoryList[section]   
    let aCategoryEntry:[String] = categoryDict[categoryKey] as! [String]
    let anObject = aCategoryEntry[row] //
    let cell.textLabel.text = anObject
    return cell
}

【讨论】:

  • 这正是我想要的——非常感谢!!我使用字典来存储数据的原因很简单——就目前而言——我有点明白我在使用它们时在做什么。效率和数据存储是我的快速学习任务清单的下一步,但现在我很高兴我可以存储我的数据。再说一遍:谢谢!
  • 如果您自己编写了在原始问题中发布的代码,那么您已经完成了 90% 的工作。在您迈出最后一步之前,您只是失去了“大局”。
猜你喜欢
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多