tableViewの新しいsectionとして日付の配列を作っています(下記コード該当部分1)。このsectionにセルをコードで付与するには、どのように書けば良いのでしょうか。付与するセルは次のとおりです。
var checkListItem: [String : Bool] = [
"アイテム1" : false,
"アイテム2" : false,
"アイテム3" : false,
"アイテム4" : false,
"アイテム5" : false
]
「swift セルをコードで作る」とネットで検索すると、以前のバージョンのコードはありましたがswift3では変換してもエラーが出て動きません。本を見てもストリーボードを使うのが前提になっています。
swift3
1import UIKit 2 3class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 6 //文字列の日付をDate型に変換するクラス 7 class DateUtils { 8 class func dateFromString(string: String, format: String) -> NSDate { 9 let formatter: DateFormatter = DateFormatter() 10 formatter.dateFormat = format 11 return formatter.date(from: string)! as NSDate 12 } 13 14 class func stringFromDate(date: NSDate, format: String) -> String { 15 let formatter: DateFormatter = DateFormatter() 16 formatter.dateFormat = format 17 return formatter.string(from: date as Date) 18 } 19 } 20 21 22 //テーブルビューのsectionに表示する日付の配列 23 var sectionTitle = ["2016-01-23","2015-12-31","2015-01-12","2016-02-21","2016-12-20"] 24 25 //今日と日付の配列の最後の日差分の日付を配列にするメソッド 26 func getDaysArrayToToday(start:String,max:Int) -> [String] { 27 //テーブルビューのsectionに表示する日付の配列の最後の日 28 let dateArraymax = sectionTitle.max() 29 30 // 日付の配列の最後の日をDate型に変換 31 let dateArraymaxdate = DateUtils.dateFromString(string: dateArraymax!, format: "yyyy/MM/dd") 32 33 //今日と日付の配列の最後の日差 34 let now = NSDate() 35 let dateDifference = Int(now.timeIntervalSince(dateArraymaxdate as Date)/60/60/24) 36 37 var result:[String] = [] 38 let formatter = DateFormatter() 39 formatter.locale = NSLocale(localeIdentifier: "ja_JP") as Locale 40 formatter.dateFormat = "yyyy-MM-dd" 41 // 今日 42 let todayStr = formatter.string(from: Date()) 43 let startDate = formatter.date(from: start)! 44 var components = DateComponents() 45 let calendar = Calendar(identifier: Calendar.Identifier.gregorian) 46 47 for i in 0 ..< max { 48 components.setValue(i,for: Calendar.Component.day) 49 let wk = calendar.date(byAdding: components, to: startDate)! 50 let wkStr = formatter.string(from: wk) 51 print(max) 52 if wkStr > todayStr { 53 break 54 } else { 55 56 result.append(wkStr) 57 } 58 let sectionTitle2 = getDaysArrayToToday(start: dateArraymax!,max: dateDifference) 59 60 //今日と日付の配列の最後の日差分の日付配列を元の日付配列に加える 61 sectionTitle.append(contentsOf: sectionTitle2)//**__該当部分1__** 62 } 63 return result 64 } 65 66 //今日と日付の配列の最後の日差分の日付配列をテーブルビューのsectionに表示する 67 68 func numberOfSections(in tableView: UITableView) -> Int // Default is 1 if not implemented 69 { 70 return sectionTitle.count 71 } 72 //// 73 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different 74 { 75 return sectionTitle[section] 76 } 77 78 79 // 80 let statusBarHeight = UIApplication.shared.statusBarFrame.height 81 82//セクションの項目 83 84 85 86 // チェックリストの項目とチェック状態 87 var checkListItem1: [String : Bool] = [ 88 "アイテム1" : true, 89 "アイテム2" : false, 90 "アイテム3" : true, 91 "アイテム4" : true, 92 "アイテム5" : false 93 ] 94 var checkListItem2: [String : Bool] = [ 95 "アイテム2-1" : false, 96 "アイテム2-2" : true, 97 "アイテム2-3" : true, 98 "アイテム2-4" : true, 99 "アイテム2-5" : false 100 ] 101 var checkListItem3: [String : Bool] = [ 102 "アイテム3-1" : true, 103 "アイテム3-2" : true, 104 "アイテム3-3" : true, 105 "アイテム3-4" : true, 106 "アイテム3-5" : false 107 ] 108 var checkListItem4: [String : Bool] = [ 109 "アイテム4-1" : true, 110 "アイテム4-2" : false, 111 "アイテム4-3" : true, 112 "アイテム4-4" : false, 113 "アイテム4-5" : false 114 ] 115 var checkListItem5: [String : Bool] = [ 116 "アイテム5-1" : true, 117 "アイテム5-2" : false, 118 "アイテム5-3" : true, 119 "アイテム5-4" : true, 120 "アイテム5-5" : true 121 ] 122 123 // 124 var tableData: [[String: Bool]] = [] 125 126 let tableView = UITableView() 127 128 // 129 override func viewDidLoad() { 130 super.viewDidLoad() 131// sectionを日付降順にソートする 132 sectionTitle = sectionTitle.sorted { $0 > $1 } 133 134 tableData = [checkListItem1, checkListItem2, checkListItem3, checkListItem4, checkListItem5] 135 136 // UITableView の作成 137 tableView.frame = CGRect( 138 x: 0, 139 y: statusBarHeight, 140 width: self.view.frame.width, 141 height: self.view.frame.height - statusBarHeight 142 ) 143 tableView.delegate = self 144 tableView.dataSource = self 145 self.view.addSubview(tableView) 146 } 147 148 // セルの作成 149 // 150 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 151 152 // tableDataの中から抽出 153 let sectionData = tableData[indexPath.section] 154 155 // キーで並び替え 156 let keys = sectionData.keys.sorted() 157 158 // キーの文字列を取得 159 let cellText = keys[indexPath.row] 160 161 // セルの作成とテキストの設定 162 let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 163 cell.textLabel?.text = cellText 164 165 /// 166 let cellIscheckd = sectionData[cellText] 167 168 // チェック状態が true なら、初めからチェック状態にする 169 if cellIscheckd == true { 170 cell.imageView?.image = UIImage(named: "checked") 171 } else { 172 cell.imageView?.image = UIImage(named: "unchecked") 173 } 174 175 return cell 176 } 177 178 // セルがタップされた時の処理 179 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 180 181 if let cell = tableView.cellForRow(at: indexPath) { 182 183 // タップしたセルのテキストを取得 184 let cellText = cell.textLabel?.text ?? "" 185 186 // 画像を切り替えと Dictonary の値を変更 187 if cell.imageView?.image == UIImage(named: "checked") { 188 189 self.tableData[indexPath.section][cellText] = false 190 191 cell.imageView?.image = UIImage(named: "unchecked") 192 } else { 193 194 self.tableData[indexPath.section][cellText] = true 195 cell.imageView?.image = UIImage(named: "checked") 196 } 197 198 // 選択状態を解除 199 cell.isSelected = false 200 } 201 } 202 203 204 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 205 let sectionData = tableData[section] 206 return sectionData.count 207 } 208} 209```swift3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/29 05:06