我正在实现一个显示文档列表的表格视图:
MyTableviewController
import UIKit
class MyTableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var editButton: UIBarButtonItem!
@IBOutlet weak var tableview: UITableView!
var animalNameArray = ["cat","dog","lion"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
}
@IBAction func editButtonAtNavigationBar(_ sender: UIBarButtonItem) {
self.tableview.isEditing = !self.tableview.isEditing
sender.title = (self.tableview.isEditing) ? "Done" : "Edit"
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animalNameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let valueAtCell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! CustomTableViewCell
valueAtCell.cellLabel?.text = animalNameArray[indexPath.row]
return valueAtCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
animalNameArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
//Rearranging the table view cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let itemMove = animalNameArray[sourceIndexPath.row]
animalNameArray.remove(at: sourceIndexPath.row)
animalNameArray.insert(itemMove, at: destinationIndexPath.row)
}
}
我该如何在删除位置使用图像(垃圾箱)?
答案 0 :(得分:1)
这是解决方案
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var editButton: UIBarButtonItem!
@IBOutlet weak var tableview: UITableView!
var animalNameArray = ["cat","dog","lion"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let defaults = UserDefaults.standard
let operationArray = defaults.stringArray(forKey: "SavedStringArray") ?? [String]()
if operationArray.count == 0 {
}else{
animalNameArray = operationArray
}
}
@IBAction func editButtonAtNavigationBar(_ sender: UIBarButtonItem) {
self.tableview.isEditing = !self.tableview.isEditing
sender.title = (self.tableview.isEditing) ? "Done" : "Edit"
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animalNameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let valueAtCell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! CustomTableViewCell
valueAtCell.cellLabel?.text = animalNameArray[indexPath.row]
return valueAtCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
animalNameArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print(animalNameArray)
let defaults = UserDefaults.standard
defaults.set(animalNameArray, forKey: "SavedStringArray")
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
//Rearranging the table view cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let itemMove = animalNameArray[sourceIndexPath.row]
animalNameArray.remove(at: sourceIndexPath.row)
animalNameArray.insert(itemMove, at: destinationIndexPath.row)
print(animalNameArray)
}
}
欢迎大家:)
下载示例
https://github.com/testingraahul/TableViewEditing/tree/master
答案 1 :(得分:1)
nvm
尝试使用此代码,然后将情节提要中的“取消”按钮连接到import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var editButton: UIBarButtonItem!
@IBOutlet weak var tableview: UITableView!
var animalNameArray = [String]() {
didSet {
UserDefaults.standard.set(animalNameArray, forKey: "savedNameArray")
}
}
var originalArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
animalNameArray = UserDefaults.standard.array(forKey: "savedNameArray") as? [String] ?? ["cat","dog","lion"]
originalArray = animalNameArray
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
@IBAction func editButtonAtNavigationBar(_ sender: UIBarButtonItem) {
self.tableview.isEditing = !self.tableview.isEditing
sender.title = (self.tableview.isEditing) ? "Done" : "Edit"
}
@IBAction func cancelButtonAtNavigationBar(_ sender: UIBarButtonItem) {
self.tableview.isEditing = false
navigationItem.rightBarButtonItem?.title = "Edit"
animalNameArray = originalArray
tableview.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animalNameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let valueAtCell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! CustomTableViewCell
valueAtCell.cellLabel?.text = animalNameArray[indexPath.row]
return valueAtCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
animalNameArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
//Rearranging the table view cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let itemMove = animalNameArray[sourceIndexPath.row]
animalNameArray.remove(at: sourceIndexPath.row)
animalNameArray.insert(itemMove, at: destinationIndexPath.row)
}
}