Swift - 使用多维数组中的多个部分填充TableView [英] Swift - Populating TableViews with multiple sections from multidimensional array

查看:187
本文介绍了Swift - 使用多维数组中的多个部分填充TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a beginner in Swift and have managed to understand at least the very basics of how to populate a UITableView. I am stuck now at populating multiple sections with data provided out of a dictionary.

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

I have a multidimensional Dictionary that assigns Objects to Categories:

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

现在我要填充我的TableView ,所以它显示如下:

Now I want to populate my TableView, so that it shows something like this:


  • A类

  • Category A


  • 对象1

  • 对象2

类别B


  • 对象3

到目前为止,我能够创建一个类别数组来返回节的数量,并计算存储在字典中的数组,以获得每个特定类别中的行数。现在我完全陷入了使用section和rows填充TableView的问题。我怎样才能做到这一点?非常感谢!

So far I am able to create an array of categories to return the number of sections as well as to count the arrays stored in the dictionary to get the number of rows in each specific category. Now I am completely stuck at populating the TableView with sections and rows. How can I do this? Thank you very much!

到目前为止我的代码:

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
}


推荐答案

首先:字典是存储表格视图内容的不良选择。字典本质上是无序的,因此您必须对键进行排序。如果您的数据超出了少量项目,那么排序过程将花费相当多的时间。

First off: A dictionary is a poor choice for storing table view contents. Dictionaries are inherently unordered, so you have to keep sorting the keys. If your data gets beyond a small number of items that sort process will take appreciable time.

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

If you are going to use a dictionary anyway you should refactor your code so that you keep the sorted keys and use them over and over unless the dictionary changes. I would make it so that you add a setter method to the dictionary and always use that setter to change the dictionary. The setter method would regenerate the sorted keys. That way you only have to sort the keys if the dictionary changes. (But better to get rid of the dictionary entirely.)

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

I would suggest creating a Section object that contains a sectionTitle String and a sectionEntries Array. Then make the table view data an array of Section objects.

但是,由于你有一本字典,我会告诉你如何使代码有效。

However, since you have a dictionary, I'll show you how to make that code work.

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

You need help with your cellForRowAtIndexPath method. You are almost there. You just need to fetch the appropriate entry in your data structure using the indexPath section and row. Something like this:

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
}

这篇关于Swift - 使用多维数组中的多个部分填充TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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