Skip to content

Instantly share code, notes, and snippets.

@muhlenXi
Created December 2, 2018 09:42
Show Gist options
  • Save muhlenXi/12cd17d87f782174f275af7fb4661c15 to your computer and use it in GitHub Desktop.
Save muhlenXi/12cd17d87f782174f275af7fb4661c15 to your computer and use it in GitHub Desktop.
一个 tableView 显示、编辑、删除的 demo
//
// FontViewController.swift
// SF
//
// Created by muhlenXi on 2018/12/2.
// Copyright © 2018 muhlenXi. All rights reserved.
//
import UIKit
fileprivate let CellIdentifier = "CellIdentifier"
class FontViewController: UIViewController {
var dataSourece = UIFont.familyNames
lazy var tableView: UITableView = {
let table = UITableView()
table.backgroundColor = UIColor.white
table.tableFooterView = UIView()
table.estimatedRowHeight = 0
table.estimatedSectionHeaderHeight = 0
table.estimatedSectionFooterHeight = 0
table.dataSource = self
table.delegate = self
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
self.setupSubviews()
}
// MARK: Private method
func setupSubviews() {
self.edgesForExtendedLayout = .bottom
self.navigationItem.title = "UIFont family"
self.view.backgroundColor = UIColor.white
tableView.register(UITableViewCell.self, forCellReuseIdentifier: CellIdentifier)
self.view.addSubview(tableView)
tableView.snp.makeConstraints { (maker) in
maker.edges.equalTo(self.view)
}
}
/// 编辑输入框
func showInputAlert(indexPath: IndexPath) {
let alertController = UIAlertController(title: "编辑", message: nil, preferredStyle: .alert)
alertController.addTextField { (tf) in
tf.text = self.dataSourece[indexPath.row]
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action) in
self.tableView.setEditing(false, animated: true)
}
let confirmAction = UIAlertAction(title: "确定", style: .default) { (action) in
if let text = alertController.textFields?.first?.text {
self.dataSourece[indexPath.row] = text
self.tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
self.present(alertController, animated: true, completion: nil)
}
}
// MARK: UITableViewDataSource
extension FontViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSourece.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath)
cell.textLabel?.text = dataSourece[indexPath.row]
return cell
}
}
// MARK: UITableViewDelegate
extension FontViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("did select section: \(indexPath.section) row: \(indexPath.row)")
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 45.0
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .normal, title: "编辑") { (action, indexpath) in
self.showInputAlert(indexPath: indexPath)
}
editAction.backgroundColor = UIColor.blue
let deleteAction = UITableViewRowAction(style: .destructive, title: "删除") { (action, indexpath) in
/// 先删除数据源,再删除 cell
self.dataSourece.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
return [deleteAction, editAction]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment