ios swift3.0显示表格需要使用UITableView对象,其中UITableViewCell是表格行的对象,下面一个简单的教程tutorials来学习下swift uitableview的使用,如下。
import UIKit
class ContactTableViewController: UITableViewController {
var datasource = NSMutableArray.init()
override func viewDidLoad() {
super.viewDidLoad()
//datasource = ["apple","orange","strawberry","banana","watermelon"];
datasource.add("apple")
datasource.add("orange")
datasource.add("strawberry")
datasource.add("banana")
datasource.add("watermelon")
}
// MARK: 以下三个方法显示UITableView数据
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init()
cell.textLabel?.text = String(describing: datasource[indexPath.row])
return cell
}
//选择行弹出提示框
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let msg = datasource[indexPath.row] as? String
let alert = UIAlertController.init(title: "提示", message:msg , preferredStyle: UIAlertControllerStyle.alert)
let action = UIAlertAction.init(title: "好的", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
//以下三个为滑动删除功能
override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "删除";
}
//删除样式
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.delete
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//编辑样式为删除UITableViewCellEditingStyle.delete
if editingStyle == .delete {
self.datasource .removeObject(at: indexPath.row)
self.tableView.reloadData()
}
}
}UITableViewCell做了一个滑动删除的效果,如图所示:
