创建像iOS日历一样的tableview

时间:2018-12-12 17:16:15

标签: ios swift uitableview swift4 fscalendar

我正在尝试创建类似于Google或iOS的日历。对于日历,我使用了令人惊叹的FSCalendar。完美运作。但是我对于日间活动的表视图有问题。我创建了带有时间轴的uitableview(1个单元格= 30分钟)。但是,例如,如何向此处添加类似于Google或iOS日历的事件 enter image description here

我认为我需要为每个事件添加按钮并将其添加到uitableview。 这是我的代码:

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)

    }
}

但是,我认为这不是一个很好的解决方案。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

对于您要完成的工作,我建议使用滚动视图。而不是使用单元格并添加视图...在单元格之间拆分它们将很棘手。相反,您可以使滚动视图具有所有的分隔线和小时标记。

实现将用作事件的自定义UIView。用户创建事件时,它将是滚动视图的子视图。假设您的滚动视图长2400像素,每小时100像素,如果事件在下午12点开始,则将UIView添加到1200像素标记...

这是我将如何解决此问题的粗略解释。如有其他问题,请随时与我们联系。