1. 程式人生 > >[Xcode10 實際操作]五、使用表格-(11)調整UITableView的單元格順序

[Xcode10 實際操作]五、使用表格-(11)調整UITableView的單元格順序

本文將演示如何調整單元格在表格中的位置。

在專案導航區,開啟檢視控制器的程式碼檔案【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.none 70 } 71 72 //新增一個代理方法,用來設定單元格是否允許拖動換行 73 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 74 return true 75 } 76 77 //新增一個代理方法,用來響應單元格的移動事件 78 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 79 //首先獲得單元格移動前的位置 80 let fromRow = sourceIndexPath.row 81 //然後獲得單元格移動後的位置 82 let toRow = destinationIndexPath.row 83 //獲得陣列在單元格移動前的物件 84 let obj = months[fromRow] 85 86 //刪除陣列中單元格移動前位置的物件, 87 months.remove(at: fromRow) 88 //然後在陣列中的目標位置,重新插入一份刪除的物件 89 months.insert(obj, at: toRow) 90 } 91 92 override func didReceiveMemoryWarning() { 93 super.didReceiveMemoryWarning() 94 // Dispose of any resources that can be recreated. 95 } 96 }