[Xcode 實際操做]5、使用表格-(10)插入UITableView單元格

目錄:[Swift]Xcode實際操做html

本文將演示如何插入一行單元格。swift

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】數組

 1 import UIKit
 2 
 3 //首先添加兩個協議。
 4 //一個是表格視圖的代理協議UITableViewDelegate
 5 //另外一個是表格視圖的數據源協議UITableViewDataSource 
 6 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 7     
 8     //建立一個數組,做爲表格的數據來源
 9     var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
10 
11     override func viewDidLoad() {
12         super.viewDidLoad()
13         // Do any additional setup after loading the view, typically from a nib.
14 
15         //建立一個位置在(0,40),尺寸爲(320,420)的顯示區域
16         let rect = CGRect(x: 0, y: 40, width: 320, height: 420)
17         //初始化一個表格視圖,並設置其位置和尺寸信息
18         let tableView = UITableView(frame: rect)
19         
20         //設置表格視圖的代理,爲當前的視圖控制器
21         tableView.delegate = self
22         //設置表格視圖的數據源,爲當前的視圖控制器
23         tableView.dataSource = self
24         //在默認狀態下,開啓表格的編輯模式
25         tableView.setEditing(true, animated: false)
26         
27         //將表格視圖,添加到當前視圖控制器的根視圖中
28         self.view.addSubview(tableView)
29     }
30     
31     //添加一個代理方法,用來設置表格視圖,擁有單元格的行數
32     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
33         //在此使用數組的長度,做爲表格的行數
34         return months.count
35     }
36     
37     //添加一個代理方法,用來初始化或複用表格視圖中的單元格
38     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
39         
40         //建立一個字符串,做爲單元格的複用標識符
41         let identifier = "reusedCell"
42         //單元格的標識符,能夠看做是一種複用機制。
43         //此方法能夠從,全部已經開闢內存的單元格里面,選擇一個具備一樣標識符的、空閒的單元格
44         var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
45         
46         //判斷在可重用單元格隊列中,是否擁有能夠重複使用的單元格。
47         if(cell == nil)
48         {
49             //若是在可重用單元格隊列中,沒有能夠重複使用的單元格,
50             //則建立新的單元格。新的單元格具備系統默認的單元格樣式,並擁有一個複用標識符。
51             cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
52         }
53         
54         //索引路徑用來標識單元格在表格中的位置。它有section和row兩個屬性,
55         //section:標識單元格處於第幾個段落
56         //row:標識單元格在段落中的第幾行
57         //獲取當前單元格的行數
58         let rowNum = (indexPath as NSIndexPath).row
59         //根據當前單元格的行數,從數組中獲取對應位置的元素,做爲當前單元格的標題文字
60         cell?.textLabel?.text = months[rowNum]
61         
62         //返回設置好的單元格對象。
63         return cell!
64     }
65     
66     //添加一個代理方法,用來設置單元格的編輯模式
67     func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
68         //在此設置單元格的編輯模式爲插入模式
69         return UITableViewCell.EditingStyle.insert
70     }
71     
72     //添加一個代理方法,用來響應單元格的插入事件
73     func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
74         //判斷編輯模式爲插入,則執行以後的代碼
75         if(editingStyle == UITableViewCell.EditingStyle.insert)
76         {
77             //獲取插入位置的單元格在段落中的行數
78             let rowNum = (indexPath as NSIndexPath).row
79             //往數組中插入新數據,及時更新數據源
80             months.insert("Honey Moon", at: rowNum)
81             
82             //建立一個包含帶插入單元格位置信息的數組
83             let indexPaths = [indexPath]
84             //往表格視圖中的指定位置,插入新單元格
85             tableView.insertRows(at: indexPaths, with: UITableView.RowAnimation.right)
86         }
87     }
88 
89     override func didReceiveMemoryWarning() {
90         super.didReceiveMemoryWarning()
91         // Dispose of any resources that can be recreated.
92     }
93 }
相關文章
相關標籤/搜索