ios - 行分隔符上的标签 - Swift Tableview - 每小时日历

标签 ios swift uitableview calendar

我想创建一个相对基本但类似于 Apple 原生日历 View 的每小时日历 View 。如何添加标签以符合行/单元格分隔符,而不包含在单元格中。像这样:

enter image description here

是否有允许您向线条添加标签的属性?标签是否必须放置在表格 View 之外?还是有一个单独的表出现?

就创建彩色 block 来表示日历上的事件而言,最好的方法是什么?它只是原型(prototype)单元格中的 CGRect 吗?您需要创建单独的 xib 文件吗?

在此先感谢您的帮助,我对学习 Swift 还是个新手!

最佳答案

这是不可能的(或者从技术上讲,这是可能的,但考虑到您的其他选择,开销太高了)。

不使用单元格分隔符,而是设置 separatorStyle = .none,并在单元格中画线(例如,作为 UIView with view.height = 1view.backgroundColor = .grey) 并且通常在单元格中添加标签。

基本上解决方案非常简单:禁用标准分隔线,而是在单元格(底部或顶部)内绘制分隔符以及标签。这就是当客户要求一些自定义花式分隔符时我一直在做的事情——我在单元格底部添加了一个自定义行,并使用单元格的 contentView 的其余部分作为单元格的内容。

编辑

您可以使用以下示例开始(请注意,这只是几种不同的管理方法之一):

class TimeCellViewController: UITableViewController {
    override func loadView() {
        super.loadView()

        // you can use UITableViewAutomaticDimension instead of static height, if
        // there will be variable heights that you don't know upfront
        // https://stackoverflow.com/a/18746930/2912282
        // or mine:
        // https://stackoverflow.com/a/47963680/2912282
        tableView.rowHeight = 80
        tableView.estimatedRowHeight = 80

        tableView.separatorStyle = .none

        // to allow scrolling below the last cell
        tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))

        tableView.register(TimeCell.self, forCellReuseIdentifier: "timeCell")
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell", for: indexPath) as! TimeCell
        if indexPath.row > 0 {
            cell.topTime = "\(indexPath.row):00"
        } else {
            cell.topTime = ""
        }
        cell.bottomTime = "\(indexPath.row + 1):00"
        return cell
    }
}

class TimeCell: UITableViewCell {
    // little "hack" using two labels to render time both above and below the cell
    private let topTimeLabel = UILabel()
    private let bottomTimeLabel = UILabel()

    private let separatorLine = UIView()

    var topTime: String = "" {
        didSet {
            topTimeLabel.text = topTime
        }
    }
    var bottomTime: String = "" {
        didSet {
            bottomTimeLabel.text = bottomTime
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        selectionStyle = .none

        contentView.addSubview(topTimeLabel)
        contentView.addSubview(bottomTimeLabel)
        contentView.addSubview(separatorLine)


        topTimeLabel.textColor = UIColor.gray
        topTimeLabel.textAlignment = .right
        bottomTimeLabel.textColor = UIColor.gray
        bottomTimeLabel.textAlignment = .right
        separatorLine.backgroundColor = UIColor.gray

        bottomTimeLabel.translatesAutoresizingMaskIntoConstraints = false
        topTimeLabel.translatesAutoresizingMaskIntoConstraints = false
        separatorLine.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            bottomTimeLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0),
            bottomTimeLabel.centerYAnchor.constraint(equalTo: self.bottomAnchor),
            bottomTimeLabel.widthAnchor.constraint(equalToConstant: 50),
            topTimeLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0),
            topTimeLabel.centerYAnchor.constraint(equalTo: self.topAnchor),
            topTimeLabel.widthAnchor.constraint(equalToConstant: 50),
            separatorLine.leftAnchor.constraint(equalTo: bottomTimeLabel.rightAnchor, constant: 8),
            separatorLine.bottomAnchor.constraint(equalTo: self.bottomAnchor),
            separatorLine.heightAnchor.constraint(equalToConstant: 1),
            separatorLine.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0),
            ])

        // if you use UITableViewAutomaticDimension instead of static height,
        // you will have to set priority of one of the height constraints to 999, see
        // https://stackoverflow.com/q/44651241/2912282
        // and
        // https://stackoverflow.com/a/48131525/2912282
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

关于ios - 行分隔符上的标签 - Swift Tableview - 每小时日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135906/

相关文章:

ios - 隐藏某个表格行 - Swift 4

ios - 在 iOS 中,在启动屏幕显示之前正在运行哪些功能

ios - 如何将 UITableView 帧一个接一个对齐并适合 UIScrollView?

ios - Reactivecocoa KVC 和实例对象之间的区别

swift - 我可以将应用程序上传到使用 Xcode 10.1 和 Swift 4.2 开发的应用程序商店吗

ios - 错误 : registering a custom tableView cell in swift with an xib file

ios - 如果超过其 indexpath.row,如何在 TableView 中设置单元格行高?

ios - 以更大的半径调用 didEnterRegion (iOS)

swift - 如何正确地等待功能快速完成?

ios - UITableViewCell 改变高度