[英]How to store an array of dictionary and display it on collection view in swift 4
[英]How to display the array in dictionary - Swift
我开始对Swift
进行编程,但遇到了一个我不知道如何解决的问题。
我做了一个Plist 文件:
<dict>
<key>Vegetables</key>
<array>
<string>Pepper</string>
<string>Tomato</string>
<string>Cucumber</string>
</array>
<key>Fruits</key>
<array>
<string>Apple</string>
<string>Banana</string>
<string>Watermelon</string>
</array>
</dict>
现在我想在TableView
的根(字典)中显示所有数组(水果和蔬菜),这样每个数组名称(键)将是一个单独的部分,每个字符串将是我的TableView
一行。
这是我的视图控制器类:
import UIKit
//I add TableView methods
class ViewController : UIViewController , UITableViewDelegate ,UITableViewDataSource {
override func viewDidLoad(){
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// return number of section in my case it's two section (fruits and vegetables)
return 1;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// number of row in every section is three
// I want to do something like Array.count
return 1;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row : UITableViewCell = UITableViewCell();
//Add every Row
return row;
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// Here i want to add header to every section i made;
// Header Name = Array (key) Fruit/Vegetables
// How i do this ?
return "";
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
字典不太适合作为表视图的数据源,因为它们是无序的。 我建议您创建一个包含您的部分的外部数组。 对于每个部分,添加一个包含标题和项目数组的字典。
或者,您可以创建一个具有标题和项目条目的自定义部分结构,并创建一个部分结构数组。
像下面这样写代码,你会得到答案,
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
var listDic : NSMutableDictionary = NSMutableDictionary()
var headerArr : NSMutableArray = NSMutableArray()
@IBOutlet var tblList : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.loadData()
self.navigationController?.navigationBar.translucent = true
self.navigationController?.navigationBar.tintColor = UIColor.grayColor()
}
func loadData()
{
if let path = NSBundle.mainBundle().pathForResource("test", ofType: "plist") // Rename with your plist file name
{
self.listDic = NSMutableDictionary(contentsOfFile: path)! // NSMutableArray(contentsOfFile: path)!
}
if(self.listDic.count > 0)
{
self.headerArr.addObjectsFromArray(self.listDic.allKeys)
}
self.tblList.reloadData()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.headerArr.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let arr = self.listDic.valueForKey(self.headerArr.objectAtIndex(section) as! String) as! NSMutableArray
return arr.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.headerArr.objectAtIndex(section) as? String
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
let arr = self.listDic.valueForKey(self.headerArr.objectAtIndex(indexPath.section) as! String) as! NSMutableArray
cell.textLabel?.text = arr.objectAtIndex(indexPath.row) as? String
return cell
}
}
这是基于您的 plist 文件并且工作正常。
希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.