[Xcode 实际操做]5、使用表格-(9)删除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         //将表格视图,添加到当前视图控制器的根视图中
26         self.view.addSubview(tableView)
27     }
28     
29     //添加一个代理方法,用来设置表格视图,拥有单元格的行数
30     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
31         //在此使用数组的长度,做为表格的行数
32         return months.count
33     }
34     
35     //添加一个代理方法,用来初始化或复用表格视图中的单元格
36     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
37         
38         //建立一个字符串,做为单元格的复用标识符
39         let identifier = "reusedCell"
40         //单元格的标识符,能够看做是一种复用机制。
41         //此方法能够从,全部已经开辟内存的单元格里面,选择一个具备一样标识符的、空闲的单元格
42         var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
43 
44         //判断在可重用单元格队列中,是否拥有能够重复使用的单元格。
45         if(cell == nil)
46         {
47             //若是在可重用单元格队列中,没有能够重复使用的单元格,
48             //则建立新的单元格。新的单元格具备系统默认的单元格样式,并拥有一个复用标识符。
49             cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
50         }
51         
52         //索引路径用来标识单元格在表格中的位置。它有section和row两个属性,
53         //section:标识单元格处于第几个段落
54         //row:标识单元格在段落中的第几行
55         //获取当前单元格的行数
56         let rowNum = (indexPath as NSIndexPath).row
57         //根据当前单元格的行数,从数组中获取对应位置的元素,做为当前单元格的标题文字
58         cell?.textLabel?.text = months[rowNum]
59 
60         //返回设置好的单元格对象。
61         return cell!
62     }
63     
64     //添加一个代理方法,用来设置单元格的编辑模式
65     func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
66         //在此设置单元格的编辑模式为删除模式
67         return UITableViewCell.EditingStyle.delete
68     }
69     
70     //添加一个代理方法,用来响应单元格的删除事件
71     func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
72         //判断若是执行模式为删除,则执行以后的代码
73         if(editingStyle == UITableViewCell.EditingStyle.delete)
74         {
75             //获取待删除的单元格,在段落中的行数
76             let rowNum = (indexPath as NSIndexPath).row
77             //从数组中将该单元格清除,以保证数据的一致性
78             months.remove(at: rowNum)
79             
80             //建立一个待删除单元格位置信息的数组
81             let indexPaths = [indexPath]
82             //再从表格视图中,清除该单元格
83             tableView.deleteRows(at: indexPaths, with: UITableView.RowAnimation.automatic)
84         }
85     }
86 
87     override func didReceiveMemoryWarning() {
88         super.didReceiveMemoryWarning()
89         // Dispose of any resources that can be recreated.
90     }
91 }
相关文章
相关标签/搜索