简体   繁体   English

Swift - 用多维数组中的多个部分填充 TableViews

[英]Swift - Populating TableViews with multiple sections from multidimensional array

I am a beginner in Swift and have managed to understand at least the very basics of how to populate a UITableView.我是 Swift 的初学者,并且至少了解了如何填充 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"]]

Now I want to populate my TableView, so that it shows something like this:现在我想填充我的 TableView,以便它显示如下内容:

  • Category A A类

    • Object 1对象 1
    • Object 2对象 2
  • Category B B类

    • Object 3对象 3

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.现在我完全陷入了用部分和行填充 TableView 的问题。 How can I do this?我怎样才能做到这一点? Thank you very much!非常感谢!

My code so far:到目前为止我的代码:

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.如果您的数据超出少量项目,则排序过程将花费相当长的时间。

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.我会这样做,以便您向字典添加一个 setter 方法,并始终使用该 setter 来更改字典。 The setter method would regenerate the sorted keys. setter 方法将重新生成已排序的键。 That way you only have to sort the keys if the dictionary changes.这样,您只需在字典更改时对键进行排序。 (But better to get rid of the dictionary entirely.) (但最好完全摆脱字典。)

I would suggest creating a Section object that contains a sectionTitle String and a sectionEntries Array.我建议创建一个包含 sectionTitle StringsectionEntries数组的Section对象。 Then make the table view data an array of Section objects.然后使表视图数据成为 Section 对象的数组。

However, since you have a dictionary, I'll show you how to make that code work.但是,由于您有字典,我将向您展示如何使该代码工作。

You need help with your cellForRowAtIndexPath method.您需要有关cellForRowAtIndexPath方法的帮助。 You are almost there.你快到了。 You just need to fetch the appropriate entry in your data structure using the indexPath section and row.您只需要使用 indexPath 部分和行在数据结构中获取适当的条目。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按该数组中的日期对Swift 4中的Tableviews部分进行排序 - Sort a Tableviews sections in Swift 4 by a date that is in that array 在Swift中用数组中的多个部分和多个字典填充TableView - Populating TableView with multiple sections and multiple dictionary in an array in Swift 在 Swift 中使用数组创建 TableView - Creating TableViews in Swift with an Array 如何在Swift中的Json文件中显示多个TableView? - How can I display multiple TableViews from a Json File in Swift? 如何在Swift 3的不同部分下显示多维数组 - How to display a multidimensional array under different sections in swift 3 仅从一个NSMutableArray填充具有多个节的tableview - populating tableview with multiple sections from only one NSMutableArray 正确填充具有多个部分的UITableView吗? - Populating a UITableView with multiple sections correctly? 单个数组的Swift Collectionview节 - Swift Collectionview Sections from a single array 从另一个视图控制器到tableViews中的View Controller的数组获取数组的值swift iOS - Getting values of an array from another view controller to View Controller in tableViews labels swift ios Swift 5 - 让 tableview 等到来自 api 调用的数据返回(使用多个 tableviews) - Swift 5 - make tableview wait until data from api call comes back (using multiple tableviews)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM