|
1
2
3
|
let array = NSArray(objects: "hangge.com","baidu.com","google.com","163.com","qq.com")let filePath:String = NSHomeDirectory() + "/Documents/webs.plist"array.writeToFile(filePath, atomically: true) |
|
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array> <string>hangge.com</string> <string>baidu.com</string> <string>google.com</string> <string>163.com</string> <string>qq.com</string></array></plist> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import UIKitclass ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{ @IBOutlet weak var tableView: UITableView! var webs:NSArray? override func viewDidLoad() { super.viewDidLoad() webs = NSArray(contentsOfFile: NSHomeDirectory() + "/Documents/webs.plist") self.tableView!.delegate = self self.tableView!.dataSource = self //创建一个重用的单元格 self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell0") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return webs!.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as UITableViewCell let url = webs![indexPath.row] as! String cell.textLabel?.text = url return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
let myArray = [ [ ["name":"航歌", "url":"hangge.com"], ["name":"百度", "url":"baidu.com"], ["name":"google", "url":"google.com"] ], [ ["name":"163", "url":"163.com"], ["name":"QQ", "url":"qq.com"] ] ] //声明一个字典 let filePath:String = NSHomeDirectory() + "/Documents/webs.plist"NSArray(array: myArray).writeToFile(filePath, atomically: true)print(filePath) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array> <array> <dict> <key>name</key> <string>航歌</string> <key>url</key> <string>hangge.com</string> </dict> <dict> <key>name</key> <string>百度</string> <key>url</key> <string>baidu.com</string> </dict> <dict> <key>name</key> <string>google</string> <key>url</key> <string>google.com</string> </dict> </array> <array> <dict> <key>name</key> <string>163</string> <key>url</key> <string>163.com</string> </dict> <dict> <key>name</key> <string>QQ</string> <key>url</key> <string>qq.com</string> </dict> </array></array></plist> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import UIKitclass ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{ @IBOutlet weak var tableView: UITableView! var webs:NSArray? override func viewDidLoad() { super.viewDidLoad() webs = NSArray(contentsOfFile: NSHomeDirectory() + "/Documents/webs.plist") self.tableView!.delegate = self self.tableView!.dataSource = self //创建一个重用的单元格 self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell0") } // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部 func tableView(tableView:UITableView, titleForHeaderInSection section:Int)->String? { return " "; } //在本例中,有2个分区 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return webs!.count; } //每个分区的记录数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let data = self.webs?[section] return data!.count } //每个单元格创建和内容赋值 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell0") let data = self.webs?[indexPath.section] let url = data![indexPath.row]["url"] as! String let name = data![indexPath.row]["name"] as! String cell.textLabel?.text = name cell.detailTextLabel?.text = url return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
原文:http://www.cnblogs.com/Free-Thinker/p/4858379.html