出来たことと今のコード
import UIKit class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var mytableView: UITableView! let initialItems: [String] = [] var kei: [Item] = Array() override func viewDidLoad() { super.viewDidLoad() mytableView.delegate = self mytableView.dataSource = self for name in initialItems { kei.append(Item(name: name, deletable: false,deletecell: false, completable: false,cellcolor: false)) kei.append(Item(name: name, deletable: false, deletecell: false, completable: false,cellcolor: false)) } } @IBAction func addbtr(_ sender: Any) { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in if let textFields = alert.textFields { for textField in textFields { //self.items.append(Item(name: textField.text!)) self.kei.append(Item(name: textField.text!,deletable: true, deletecell: true,completable: false, cellcolor: true)) } self.kei.append(Item(name: textField.text!,deletable: true, deletecell: true,completable: false, cellcolor: true)) self.mytableView.reloadData() } }) alert.addAction(okAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() self.present(alert, animated: true, completion: nil) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return kei.count } func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { switch indexPath.row { case 1: return indexPath; default: return nil; } } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) //追加した内容に対して let item = kei[indexPath.row] cell.textLabel?.text = item.name if cell.textLabel?.text != "cell 追加 ボタン" { cell.accessoryType = item.complete ? .checkmark : .none cell.textLabel?.textColor = UIColor.black cell.backgroundColor = UIColor.yellow }else { cell.accessoryType = .none cell.textLabel?.textColor = UIColor.red cell.backgroundColor = UIColor.black } if indexPath.row == 0 { cell.textLabel?.textColor = UIColor.black cell.backgroundColor = UIColor.white cell.accessoryType = .none } return cell } func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath) if cell?.textLabel?.text != "cell 追加 ボタン"{ kei[indexPath.row].toggle() tableView.deselectRow(at: indexPath, animated: true) tableView.reloadRows(at: [indexPath], with: .none) return nil }else { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in if let textFields = alert.textFields { for textField in textFields { self.kei[indexPath.row] = (Item(name: textField.text!,deletable: false, deletecell: true,completable: false, cellcolor: false)), at: indexPath.row) } self.mytableView.reloadData() } }) alert.addAction(okAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() / self.present(alert, animated: true, completion: nil) return indexPath } } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { self.kei.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) } } //func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let delete = UIContextualAction(style: .normal,title: "削除", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in self.kei.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) success(true) }) let edit = UIContextualAction(style: .normal,title: "編集", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in if let textFields = alert.textFields { for textField in textFields { self.kei[indexPath.row] = (Item(name: textField.text!,deletable: false, deletecell: true,completable: false, cellcolor: false)) //self.kei.append(textField.text!) } self.mytableView.reloadData() } }) alert.addAction(okAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() self.present(alert, animated: true, completion: nil) print("edit") success(true) }) delete.backgroundColor = .red edit.backgroundColor = .blue let swipeAction = UISwipeActionsConfiguration(actions: [delete]) swipeAction.performsFirstActionWithFullSwipe = false return UISwipeActionsConfiguration(actions: [delete,edit]) } // trueを返すことでCellのアクションを許可しています func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { if kei.count > (indexPath.row + 1) { return true }else{ return false } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } //左から右へスワイプ func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let delete = UIContextualAction(style: .normal,title: "削除", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in self.kei.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) success(true) }) let edit = UIContextualAction(style: .normal,title: "編集", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { for textField in textFields { self.kei[indexPath.row] = (Item(name: textField.text!,deletable: false, deletecell: true,completable: false, cellcolor: false)) } self.mytableView.reloadData() } }) alert.addAction(okAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() self.present(alert, animated: true, completion: nil) print("edit") success(true) }) delete.backgroundColor = .orange edit.backgroundColor = .blue return UISwipeActionsConfiguration(actions: [delete,edit]) } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/10/23 02:04
2017/10/23 13:12
退会済みユーザー
2017/10/24 05:15
2017/10/24 05:30
退会済みユーザー
2017/10/24 05:57
2017/10/24 06:18
退会済みユーザー
2017/10/24 06:41
2017/10/24 14:53
退会済みユーザー
2017/10/28 13:12
退会済みユーザー
2017/10/28 13:21
退会済みユーザー
2017/12/09 12:24 編集
2017/12/09 15:12