TableViewでスワイプまたは、EditModeで削除・追加する方法を試行錯誤しています。
現状ですと、追加もできておりませんし、Deleteを押したら固まったまま動かなくなります。
知識のあるかた、ご教授よろしくお願いいたします。
swift
1import UIKit 2 3 4 5 6class example: UITableViewController{ 7 8 9 @IBOutlet var tableview: UITableView! 10 11 12 struct Objects { 13 var sectionName : String! 14 var sectionObjects : [String]! 15 } 16 17 var objectsArray = [Objects]() 18 19 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 24 25 navigationItem.rightBarButtonItem = editButtonItem() 26 27 28 objectsArray = [Objects(sectionName: "A", sectionObjects: ["aaa", "aaaa", "aaaaa"])] 29 30 31 32 // Do any additional setup after loading the view. 33 34 } 35 36 //------------------------------------------- 37 38 39 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 40 let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell! 41 42 cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row] 43 44 return cell 45 } 46 47 48 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 49 return objectsArray[section].sectionObjects.count 50 } 51 52 53 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 54 return objectsArray.count 55 } 56 57 override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 58 return objectsArray[section].sectionName 59 } 60 61 62 //----------------------------------- 63 64 65 override func setEditing(editing: Bool, animated: Bool) { 66 super.setEditing(editing, animated: animated) 67 tableView.editing = editing 68 } 69 70 override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 71 return true 72 } 73 74 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 75 76 objectsArray.removeAtIndex(indexPath.row) 77 78 tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], 79 withRowAnimation: UITableViewRowAnimation.Fade) 80 } 81 82 83 84 85 override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 86 return true 87 } 88 89 90 //------------------------------------ 91 92 93 override func didReceiveMemoryWarning() { 94 super.didReceiveMemoryWarning() 95 // Dispose of any resources that can be recreated. 96 97 98 } 99} 100 101
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。