昨天將資料存進Realm裡了,今天就來將資料顯示出來吧。
首先用TableView來顯示Realm裡的的資料,包含時間和鈴聲。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let realm = try! Realm()
let cells = realm.objects(AlarmRealm.self)
return cells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AlarmTableViewCell
let realm = try! Realm()
let cells = realm.objects(AlarmRealm.self)
cell.timeLabel.text = String(format: "%2d:%02d", arguments: [cells[indexPath.row].Hour, cells[indexPath.row].Minute])
cell.timeLabel.font = cell.timeLabel.font.withSize(60)
cell.soundLabel.text = addPageValue.pageValue.soundSource[cells[indexPath.row].soundSelect]
return cell
}
顯示的部分差不多先到這裡,接下來是新增鬧鐘。
這裡是設定navigation的Button來新增。
func navigationRightButton() {
let rightButton = UIBarButtonItem(title: nil, style: .plain, target: self, action: #selector(addAlarm))
rightButton.tintColor = .blue
rightButton.image = UIImage(systemName: "plus")
self.navigationItem.rightBarButtonItem = rightButton
}
這是Button的功能。
@objc func addAlarm() {
let addPageVC = AddPageViewController()
addPageValue.pageValue.addStruct = true
addPageValue.pageValue.repeatDayCheck = [false, false, false, false, false, false, false]
addPageValue.pageValue.labelText = ""
addPageValue.pageValue.soundSelect = 0
addPageValue.pageValue.switchOnOff = true
self.navigationController?.pushViewController(addPageVC, animated: true)
}
那今天到這裡就完成了,明天來處理剩下的部分。