ios - 创建像 iOS 日历一样的 TableView

标签 ios swift uitableview swift4 fscalendar

我正在尝试创建像 Google 或 iOS 那样的日历。对于日历,我使用了惊人的 FSCalendar。它完美地工作。但是我对日间事件的表格 View 有疑问。我用时间轴创建了 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)

    }
}

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

最佳答案

我建议您使用 ScrollView 来完成您要完成的任务。而不是使用单元格并添加 View ......在单元格之间拆分它们将很棘手。相反,您可以拥有包含所有分隔线和小时标记的 ScrollView 。

实现一个将用作事件的自定义 UIView。当用户创建事件时,它将成为您的 ScrollView 的 subview 。比如说你的 ScrollView 是 2400px 长,每小时是 100px,如果事件在中午 12 点开始,将 UIView 添加到 1200px 标记...

这是对我将如何处理这个问题的粗略解释。如果您还有其他问题,请随时联系我们。

关于ios - 创建像 iOS 日历一样的 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53748172/

相关文章:

iOS应用商店因微信登录需要安装应用而被拒绝;手机端可以使用Web View 登录微信吗?

ios - Opentok 和 CallKit 问题

ios - Swift 中漂亮的日期间隔。 NS日历组件

ios - 如何从 UITableView 的每一行读取数据/测试?

ios - 在没有 tableView 的情况下更改 UITableView 部分标题 :titleForHeaderInSection

ios - Swift iOS - 如何将直接从 TableViewCell 的子类中获取的值传递给 TableView 的 didSelectRow?

iphone - #error 1107 在 iPhone 上运行 flash 游戏时,使用 Adob​​e Air

ios - 在 Restkit 中使用 NSURLSessionDataTask

swift - 测量调用方法 "didUpdateValueFor"之间的时间

ios - UITableView 标题中的按钮仅在 1/3 的时间内起作用