I'm trying to create a calendar like as Google or iOS has. For calendar I'm using amazing FSCalendar. It work perfectly. But I have a problem with tableview for day events. I created uitableview with timeline (1 cell = 30 minutes). But how to add events to here that look like Google or iOS calendar, for example
I think I need to add buttons for each events and add it to uitableview. This is my code:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (hour_end - hours_begin) * step
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.timeLabel.layer.zPosition = 1
cell.timeLabel.isHidden = true
if indexPath.row % step == 0 && indexPath.row != ((hour_end - hours_begin) * step) - 1 {
let hour = indexPath.row / step + hours_begin
cell.timeLabel.text = (hour < 10 ? "0": "") + String(hour) + ":00"
cell.timeLabel.layer.zPosition = 10
cell.timeLabel.isHidden = false
cell.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
}
else {
cell.timeLabel.text = ""
cell.timeLabel.isHidden = true
cell.separatorInset = UIEdgeInsets(top: 0, left: 2000, bottom: 0, right: 0)
cell.timeLabel.layer.zPosition = 1
}
add_event(indexPath: indexPath, nRef: 5, offset: 0, height: 64.0)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
func add_event(indexPath: IndexPath, nRef: Int, offset: CGFloat, height: CGFloat)
{
let row = indexPath.row
let rectOfCellInTableViewCoordinates = tableView.rectForRow(at: indexPath)
let rectOfCellInSuperviewCoordinates = view.convert(rectOfCellInTableViewCoordinates, to: tableView.superview)
print("\(row) \(rectOfCellInSuperviewCoordinates.origin.x) \(rectOfCellInSuperviewCoordinates.origin.y)")
if row == nRef {
if let z = view.viewWithTag(nRef) as! UIButton?{
print("z found")
z.removeFromSuperview()
}
let btn_x = 50
let btn_width = tableView.frame.width - 50
let frame = CGRect(x: CGFloat(btn_x), y: rectOfCellInSuperviewCoordinates.origin.y + offset, width: btn_width, height: height)
let overlay_btn = UIButton(frame: frame)
overlay_btn.backgroundColor = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 0.3)
overlay_btn.setTitle("Press for more details", for: .normal)
overlay_btn.setTitleColor(UIColor.black, for: .normal)
overlay_btn.layer.cornerRadius = 8
overlay_btn.tag = 5
tableView.addSubview(overlay_btn)
}
}
But, I think it is not great solution. Any ideas?
I would recommend using a scroll view for what you are trying to accomplish. Instead of using cells and adding views... splitting them up between cells will be tricky. Instead, you can have your scroll view that has all of the dividers and hour marks.
Implement a custom UIView that will serve as an event. When the user creates an event, it will be a subview of your scrollview. Say for example your scrollview is 2400px long and each hour is 100px, If the event starts at 12pm, add the UIView to the 1200px mark...
That is a rough explanation of how I would approach this problem. Feel free to reach out if you have further questions.