正如标题一样,我正在寻找一个解决方案,包括在工作日添加开放和关闭时间。
图片myApp:
View after selecting the day of the week, (View add hours)
代码:
import UIKit
import CoreData
class OpeningHoursInfoController: UITableViewController, AddDayHouerControllerDelegate {
let daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var keyCell = "celllllId"
var hoursArray = [HourInfo]()
override func viewDidLoad() {
super.viewDidLoad()
fetchOpenAndCloseHour()
tableView.separatorStyle = .singleLine
tableView.separatorColor = .lightGold
tableView.tableFooterView = UIView()
tableView.register(OpeningHoursInfoCell.self, forCellReuseIdentifier: keyCell)
}
private func fetchOpenAndCloseHour() {
let context = CoreDataManager.shared.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<HourInfo>(entityName: "HourInfo")
do {
let hours = try context.fetch(fetchRequest)
hours.forEach({ (houers) in
})
self.hoursArray = hours
self.tableView.reloadData()
} catch let fetchErr {
print("Failed to fetch error in CoreData:", fetchErr)
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return daysOfWeek.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: keyCell, for: indexPath) as! OpeningHoursInfoCell
let days = daysOfWeek[indexPath.row]
cell.textLabel?.text = days
cell.textLabel?.textColor = .white
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let day = self.daysOfWeek[indexPath.row]
let addDayHouersController = AddDayHouerController()
addDayHouersController.delegate = self
addDayHouersController.navigationItem.title = day
navigationController?.pushViewController(addDayHouersController, animated: true)
}
// MARK: - AddDayHouerControllerDelegate Methods
func didAddHour(hour: HourInfo) {
hoursArray.append(hour)
let row = hoursArray.index(of: hour)
let reloadIndexPath = IndexPath(row: row!, section: 0)
tableView.reloadRows(at: [reloadIndexPath], with: .middle)
}
}
Code AddDayHouerController:
import UIKit
import CoreData
protocol AddDayHouerControllerDelegate {
func didAddHour(hour: HourInfo)
}
class AddDayHouerController: UIViewController {
var delegate: AddDayHouerControllerDelegate?
// MARK: - Widok
override func viewDidLoad() {
super.viewDidLoad()
// Przycisk NavBar
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, t
}
@objc func handleSave() {
let context = CoreDataManager.shared.persistentContainer.viewContext
let hourInfo = NSEntityDescription.insertNewObject(forEntityName: "HourInfo", into: context)
hourInfo.setValue(hourFrom, forKey: "openHour")
hourInfo.setValue(hourTo, forKey: "closeHour")
do {
try context.save()
dismiss(animated: true, completion: {
self.delegate?.didAddHour(hour: hourInfo as! HourInfo)
})
} catch let errSave {
print("Failed to save CoreData:", errSave)
}
}
// Picker View Target
@objc func handleAddHour(button: UIButton) {
view.layoutIfNeeded()
UIView.animate(withDuration: 0.25, animations: {
self.view.layoutIfNeeded()
}) { (completed) in
UIView.animate(withDuration: 0.25, animations: {
self.timePickerView.alpha = 1
self.timePickerView.tag = button.tag
self.view.layoutIfNeeded()
})
}
}
let timePickerView: UIDatePicker = {
let pv = UIDatePicker()
pv.timeZone = .current
pv.datePickerMode = .time
pv.addTarget(self, action: #selector(handleChoiceTime), for: .valueChanged)
return pv
}()
var hourFrom = ""
var hourTo = ""
@objc func handleChoiceTime(sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.timeStyle = .short
let selectedTime = formatter.string(from: sender.date)
if timePickerView.tag == 0 {
openFromTimeButton.setTitle(selectedTime, for: .normal)
hourFrom = selectedTime
print(hourFrom)
} else {
openToTimeButton.setTitle(selectedTime, for: .normal)
hourTo = selectedTime
print(hourTo)
}
timePickerView.alpha = 0
}
}()
}
} // End class
如何实现这种效果? (例如 - &#34;星期一:上午9:00 - 下午5:00&#34;)
答案 0 :(得分:0)
您需要一个合适的数据模型,例如一个类来获取引用语义
class DayOfWeek {
let name : String
var openHour : Date?
var closeHour : Date?
init(name : String) {
self.name = name
}
}
然后创建数据源数组
let daysOfWeek = [DayOfWeek(name:"Monday"),
DayOfWeek(name:"Tuesday"),
DayOfWeek(name:"Wednesday"),
DayOfWeek(name:"Thursday"),
DayOfWeek(name:"Friday"),
DayOfWeek(name:"Saturday"),
DayOfWeek(name:"Sunday")]
而不是hoursArray
使用类中的Date
属性或声明类
class DayOfWeek {
let name : String
var hourInfo : HourInfo?
init(name : String) {
self.name = name
}
}
在cellForRow
中分配name
let currentDay = daysOfWeek[indexPath.row]
cell.textLabel?.text = currentDay.name
当呈现AddDayHouerController
时,将相应索引路径的实例传递给控制器并设置小时数。由于引用语义(此实例和数据源数组中的实例指向同一对象),您不需要将更改的项目传回。
注意:为什么要注册OpeningHoursInfoCell
?如果单元格是在Interface Builder的表格视图中设计的,并且您没有使用额外的XIB,则不能注册该单元格。