CustomCell.swift
swift
1コード 2import UIKit 3 4class CustomCell: UITableViewCell { 5 6 @IBOutlet weak var indexLabel: UILabel!//indexLbel 7 @IBOutlet weak var minute: UILabel!//cuntLable 8 @IBOutlet weak var distance: UILabel!//priceLabel 9 10 11 var data: DataModel! { 12 didSet { 13 minute.text = data.count 14 distance.text = data.price 15 } 16 } 17}
Viewcontroller.swfit
swift
1コード 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 //var sectionTitleArray = [String]() 9 var dataArrayGroup: [[DataModel]] = [] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 tableView.estimatedRowHeight = 44 15 tableView.rowHeight = UITableViewAutomaticDimension 16 tableView.tableFooterView = UIView() 17 } 18 19 @IBAction func tapcell(_ sender: Any) { 20 if dataArrayGroup.count == 0 { 21 return 22 } 23 24 let alert = UIAlertController(title:"分×距離", 25 message: "イメージ)20 × 5", 26 preferredStyle: .alert) 27 28 let cancelAction = UIAlertAction(title: "キャンセル", 29 style: .cancel, 30 handler: 31 { action -> Void in 32 print("Cancel") 33 }) 34 35 let count = dataArrayGroup[0].count 36 let defaultAction = UIAlertAction(title: "追加", 37 style: .default, 38 handler: 39 { action -> Void in 40 41 // TextFieldから値を取得 42 if let textFields = alert.textFields { 43 44 let data = DataModel() 45 46 for textField in textFields { 47 48 if let text = textField.text, !text.isEmpty, let _ = Int(text) { 49 if textField.tag == 1 { 50 data.count = text 51 } else { 52 data.price = text 53 } 54 } 55 } 56 57 self.dataArrayGroup[0].insert(data, at: count) 58 self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic) 59 } 60 }) 61 62 63 alert.addTextField { 64 $0.placeholder = "分" 65 $0.keyboardType = .numberPad 66 $0.tag = 1 67 } 68 alert.addTextField { 69 $0.placeholder = "距離" 70 $0.keyboardType = .numberPad 71 $0.tag = 2 72 } 73 74 alert.addAction(cancelAction) 75 alert.addAction(defaultAction) 76 present(alert, animated: true, completion: nil) 77 } 78 79 // MARK: - TableView Delegate & DataSource 80 //この部分です。 81 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 82 if dataArrayGroup.count == 0 { 83 return nil 84 } else { 85 return dataArrayGroup[DataModel] 86 } 87 } 88 89 // Section Count 90 func numberOfSections(in tableView: UITableView) -> Int { 91 return dataArrayGroup.count 92 } 93 94 // Row Count 95 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 96 return dataArrayGroup[section].count 97 } 98 99 // Generate Cell 100 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 101 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell 102 cell.data = dataArrayGroup[indexPath.section][indexPath.row] 103 cell.indexLabel.text = String(indexPath.row + 1) 104 return cell 105 } 106 107 // Select Cell 108 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 109 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 110 } 111 112 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 113 if editingStyle == UITableViewCellEditingStyle.delete { 114 dataArrayGroup[indexPath.section].remove(at: indexPath.row) 115 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 116 } 117 } 118 119 //cellが削除が可能なことを伝える 120 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 121 return UITableViewCellEditingStyle.delete; 122 } 123 124}
DataModel.swift
swift
1コード 2import Foundation 3 4class DataModel { 5 6 var count: String = "0" 7 var price: String = "0" 8}
tap cellのボタンでcellを追加出来るようにしたいのですがエラーが起きてしまいました
それと日付のlabelをcellが追加される時に自動的に取得出来るようにしたいです
5と2はcellが追加時にアラートテキストで入力出来るようにしたいです。
ここは質問をするサイトです。あなたの要望を書く場所ではありません。