如何在iphone日历应用程序中创建日历视图

时间:2017-01-12 21:24:43

标签: uitableview swift3

使用tableView,我可以构建日历。我在覆盖顶部的视图以表示特定时间的时间间隔时遇到问题。下面的代码显示了初始叠加层,但在滚动表时它不跟踪引用单元格。感谢一些指导。请参阅Apple Calendar App的第二个屏幕截图。这就是我想模仿的东西

//
//  CalendarViewController.swift
//
//  Created by Syed Tariq on 01/12/17.
//  Copyright © 2016 com.syedtariq. All rights reserved.
//

import UIKit

class CalendarViewController: UIViewController,
    UITableViewDelegate,
    UITableViewDataSource
{

    let cellSize = 60
    var tableView: UITableView!
    let cellIdentifier = "cell"


    override func viewDidLoad() {
        super.viewDidLoad()


        do {
            tableView = UITableView(frame: view.frame)
            view.addSubview(tableView)
        }

        do {
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
            tableView.delegate = self
            tableView.dataSource = self
            tableView.separatorStyle = .none // turn off so the UIView extension can draw the separator line
            tableView.backgroundColor = UIColor.clear
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()



    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 25
    }

    var yCell = CGFloat(0)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:cellIdentifier, for: indexPath)
        var row = indexPath.row
        cell.backgroundColor = UIColor.white

        var post = "AM"
        if row > 12 { row = row - 12; post = "PM" }
        if row == 0 { row = 12; post = "AM"}
        let x = String(format: "%2ld", row)
        cell.textLabel?.text = x + " " + post + " " + String(repeating: "\u{2500}", count: 16)
        showOverlay(indexPath: indexPath, nRef: 5, offset: 0, height: 44.0) // show button cover a cell
        return cell
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    }


    func showOverlay(indexPath: IndexPath, nRef: Int, offset: CGFloat, height: CGFloat) {
        // overlay is a button
        // nRef is the closest cell from which a button is to be placed
        // offset is the top of the button from the top of the nRef cell
        // height is the height of the button based on the time interval to be displayed and calculated based on
        //      height of the cell

        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 overlayStart = tableView.frame.origin.x
            let overlayWidth = tableView.frame.width
            let frame = CGRect(x: overlayStart, y: rectOfCellInSuperviewCoordinates.origin.y + offset, width: overlayWidth, height: height)
            let overlayButton = UIButton(frame: frame)

            overlayButton.backgroundColor = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 0.3)
            overlayButton.setTitle("Press for more details", for: .normal)
            overlayButton.setTitleColor(UIColor.black, for: .normal)
            overlayButton.layer.cornerRadius = 8
            overlayButton.layer.borderColor = UIColor.black.cgColor
            overlayButton.layer.borderWidth = 2
            overlayButton.tag = 5
            view.addSubview(overlayButton)

        }
    }

}

Simulator capture of calendar view

From Apple Calendar App that I want to mimic

1 个答案:

答案 0 :(得分:1)

您正在主视图上添加叠加层。

这意味着当您滚动时,所有tableViewCells都会更改视图中的位置,叠加层不会知道某些内容发生了变化,因此不会更改它在视图中的位置。

一个选项:

将叠加层作为子视图添加到tableViewCell。这样,当您滚动时,叠加层将位于tableViewCell的边界内。