自定义日历视图

2023-12-22

I want to develop custom calendarView for my app. i think i can make it by use of UICollectionView like this:
enter image description here

我尝试了这个简单的事情:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 5 //for number of weeks
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 7 //for number of days in week
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AvailabilityCollectionViewCell", for: indexPath) as! AvailabilityCollectionViewCell
    cell.lblDate.text = "\(indexPath.row)"
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 0.5
    return cell
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{

    return 0.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: collectionView.frame.size.width / 7.0, height: collectionView.frame.size.height / 5)
}

通过这个我得到简单的集合视图,如下所示:

现在我想知道如何根据日历安排工作日以及需要什么样的数据源......

Thanks..


变量:

var weeks = 0
var items = [[Date]]()
lazy var dateFormatter: DateFormatter = {
    let formatter  = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter
}()

设置日历功能:

    func setCalendar() {
        let cal = Calendar.current
        let components = (cal as NSCalendar).components([.month, .day,.weekday,.year], from: date)
        let year =  components.year
        let month = components.month
        let months = dateFormatter.monthSymbols
        let monthSymbol = (months![month!-1])
        lblMonth.text = "\(monthSymbol) \(year!)"

        let weekRange = (cal as NSCalendar).range(of: .weekOfMonth, in: .month, for: date)
        let dateRange = (cal as NSCalendar).range(of: .day, in: .month, for: date)
        weeks = weekRange.length
        totalDaysInMonth = dateRange.length

        let totalMonthList = weeks * 7
        var dates = [Date]()
        var firstDate = dateFormatter.date(from: "\(year!)-\(month!)-1")!
        let componentsFromFirstDate = (cal as NSCalendar).components([.month, .day,.weekday,.year], from: firstDate)
        firstDate = (cal as NSCalendar).date(byAdding: [.day], value: -(componentsFromFirstDate.weekday!-1), to: firstDate, options: [])!

        for _ in 1 ... totalMonthList {
            dates.append(firstDate)
            firstDate = (cal as NSCalendar).date(byAdding: [.day], value: +1, to: firstDate, options: [])!
        }
        let maxCol = 7
        let maxRow = weeks
        items.removeAll(keepingCapacity: false)
        var i = 0
//        print("-----------\(monthSymbol) \(year!)------------")
        for _ in 0..<maxRow {
            var colItems = [Date]()
            for _ in 0..<maxCol {
                colItems.append(dates[i])
                i += 1
            }
//            print(colItems)
            items.append(colItems)
        }
//        print("---------------------------")
    }  

代表职能:

extension AvailabilityCalenderViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return weeks
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 7
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calendarCell", for: indexPath) as! calendarCell
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 0.5
    cell.configureCell(date: items[indexPath.section][indexPath.row])
    return cell
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
    return 0.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.size.width / 7.0, height: collectionView.frame.size.height / 5)
}
}

自定义集合视图单元格:

    class calendarCell: UICollectionViewCell {

    var date: Date!

    @IBOutlet weak var lblDate: UILabel!

    func configureCell(date: Date){
        self.strDate = date
        let cal = Calendar.current
        let components = (cal as NSCalendar).components([.day], from: date)
        let day = components.day!

        self.lblDate.text = "\(String(describing: day))"  
    }    
}

结果 :

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义日历视图 的相关文章

  • iOS 上 Safari 中的 shift 键

    有没有办法在javascript中判断手机键盘上是否按下了shift键 并将其与大写锁定 按两次shift键 区分开来 一些事实 首先 让我们看一下有关 iOS 键盘的一些事实 我假设您已经知道了 当您进入键盘模式时 shift键始终处于激
  • Swift try inside Objective-C 块

    我需要创建一个函数foo它接受一个抛出闭包作为参数 我可以使用 Swift 或 ObjC 来实现它 但我需要能够从两者中调用它 像这样 Swift func bar throws func foo block throws gt void
  • 从 ios 应用程序到 symfony2 Web 应用程序的登录和管理会话

    我使用 Symfony2 开发了一个 Web 应用程序 我用过FOS用户包用于用户管理和安全 现在 我正在考虑为我的网络开发一个本机 iOS 应用程序 但并不真正知道如何解决从应用程序创建会话的问题 并在整个交互用户应用程序中维护它 换句话
  • 在 iOS 7 中查看 Core Data 创建的 sqlite 文件时出现问题

    当我尝试访问由核心数据创建的数据库文件时遇到问题 DB 文件位于 Documents 文件夹中 我已将其复制到桌面并使用 Firefox 插件打开它 Hello sqlite文件不包含任何行我有使用核心数据插入值 我正在使用 iOS 7 模
  • supportedInterfaceOrientations 方法不会重写其超类中的任何方法

    在 UIViewController 中 这段代码 public override func supportedInterfaceOrientations gt UIInterfaceOrientationMask if let mainC
  • 更改目录时 Gitlab CI 运行程序作业失败退出状态 1

    我正在使用我的个人机器作为使用 Fastlane 的 iOS 项目的运行程序 这主要是因为共享运行器没有为 iOS 设置 因为它们没有安装 Xcode 更改目录时我的作业立即失败 它是一个 shell 运行程序 根本没有其他自定义配置 有什
  • Swift:Tableview 在导航栏下方滚动但在状态栏上方滚动?

    我使用以下技巧隐藏了导航栏的阴影 self navigationController navigationBar setBackgroundImage UIImage for default self navigationControlle
  • 与 parse-server 和 auth0 的自定义身份验证集成

    我想将 auth0 com 与开源解析服务器结合使用 我当前的方法是通过 iOS 的 Lock 库使用标准登录从 auth0 获取令牌 使用该令牌 我想在解析服务器上调用自定义身份验证方法 该方法检查令牌是否有效 如果有效则将登录用户 我的
  • 在 Swift 中的 For 循环中更改对象的属性

    我创建了一个名为 ShoppingList 的简单结构 struct ShoppingList var shoppingListId NSNumber var title String var groceryItems GroceryIte
  • Apple 由于崩溃而拒绝了我的应用程序,无法重现它

    我刚刚上传了一个应用程序到应用程序商店 它是为ios 7开发的 他们拒绝了该应用程序 因为我无法重现崩溃 他们向我发送了这份崩溃报告 Exception Type EXC BAD ACCESS SIGSEGV Exception Subty
  • 无法在 xcode 8 beta 6 上编译 AWS CustomIdentityProvider

    我在 iOS 应用程序中使用 Amazon Cognito 和 Facebook 登录 直到 beta 5 为止此代码从这个SO线程 https stackoverflow com questions 37597388 aws cognit
  • iOS 7 NS 单线程安全合并冲突

    重新排序两行后 在单线程应用程序上保存简单的数据时遇到问题 我已经成功地简化了编码以重现错误 并且希望其他人尝试这一点时得到第二个意见 这是一次健全性检查 因为我怀疑 iOS 7 引入的核心数据问题 而这在 iOS 6 中工作正常 首先 启
  • “无法取消归档名为 UITableViewController 的元素”

    我一直在按照 构建你的第二个 iOS 应用程序 教程一步步进行 在教程承诺所有错误都会消失之后 我遇到了这个错误 但直到其他错误都出现后 该错误才出现 全部更正 我尝试编译它 错误 The document MainStoryboard i
  • iOS绘图3D图形库[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在搜索一个可以帮助我绘制 3D 图表的库 我想要类似的东西这一页 http www math uri edu bkaskosz fla
  • Swift 中通过可选绑定进行安全(边界检查)数组查找?

    如果我在 Swift 中有一个数组 并尝试访问超出范围的索引 则会出现一个不足为奇的运行时错误 var str Apple Banana Coconut str 0 Apple str 3 EXC BAD INSTRUCTION 但是 我会
  • Swift 单元测试 - 如何断言 CGColor 是它应该的样子?

    使用 Xcode V7 2 尝试进行单元测试 需要验证是否已设置正确的颜色 并收到以下消息 Cannot invoke XCTAssertEqual with an argument list of type CGColor CGColor
  • AdMob 和 DFP 广告联盟之间的区别?

    我正在尝试在我的 iOS 应用程序上显示横幅广告和插页式广告 但现在我对广告网络感到困惑 AdMob 与 DFP 有何不同 哪一种更适合投放广告 有人可以提供帮助吗 提前致谢 AdMob 是一个广告网络 作为发布商 您可以通过展示从网络投放
  • iOS:如何创建核心数据库的备份副本?以及如何导出/导入该副本?

    我想为我的应用程序的用户提供创建核心数据数据库备份的可能性 特别是在他切换到新设备等情况下 我该怎么做呢 特别是如何重新导入该文件 我的意思是 假设他制作了数据库的备份副本 然后更改了大量内容并想要重置为以前保存的备份副本 我该怎么做呢 T
  • 如何在 SwiftUI 中延迟动画?

    我想为两个文本字段设置动画 第二个字段有延迟 但它不起作用 没有延迟 它们同时从位置 100 动画到 0 这是代码 State private var offset CGFloat 100 State private var offset2
  • 隐藏 UITableview 单元格

    我正在尝试从 UITableView 中隐藏单元格 就像删除操作一样 但我只想隐藏它以便稍后在相同位置显示它 我知道 UITableViewCell 有一个名为 隐藏 的属性 但是当我使用此属性隐藏单元格时 它会隐藏但没有动画 并且会留下空

随机推荐

  • r 中的错误消息:没有要聚合的行[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我正在运行一个用 r 语言编写的程序 该程序旨在将许多 csv 数据文件编译成一个 csv 文件 然后生成一个输出文件 其中包含对组合文件中
  • 在 Google Cloud 中安装 Label Studio 并使其可通过公共 IP 使用

    我在 Google Cloud 中有一个使用 Ubuntu 20 04 LTS 的虚拟机实例 我将其设置为允许 HTTP 流量 我需要设置 Label Studio https github com heartexlabs label st
  • IE 8.渐变背景+图像

    如何让IE8显示渐变背景 图像 这在其他浏览器中是可行的 但在 IE 中不起作用 在其他浏览器中 Opera background image url gxt images my eye png o linear gradient top
  • Ionic 2 - 同时多个菜单(右 - 左)

    情况 我的 Ionic 2 应用程序中有一个可用的右侧菜单 我需要添加一个左侧菜单 我已经尝试过 但到目前为止还没有成功 这是我的尝试 THE CODE 使用此代码 右侧菜单可以正常工作 但左侧菜单不会出现 应用程序 html
  • Suave - 控制何时“缓存”或重新计算响应

    我想了解如何控制响应何时 缓存 以及何时 重新计算 举个例子
  • Spring Data JPA:嵌套实体的批量插入

    我有一个测试用例 需要将 100 000 个实体实例保存到数据库中 我当前使用的代码可以执行此操作 但最多需要 40 秒才能将所有数据保留在数据库中 数据是从大小约为 15 MB 的 JSON 文件中读取的 现在我已经在另一个项目的自定义存
  • FFMPEG -filter_complex 绘制文本,样式如粗体斜体和下划线

    我正在尝试在视频的填充区域添加文本 有 4 到 5 件事我无法做 1 绘制文本样式 粗体 斜体 下划线 2 填充区域不透明度 3 字幕垂直对齐 当我给 VAlign 一些值时 有时会跑出窗外 如何正确计算 距离顶部 50 像素 或者距离底部
  • 如何通过我的 Web 服务运行 .exe 文件?

    复制 是否可以从 Web 服务运行可执行文件 https stackoverflow com questions 717657 is it possible to run an executable from a web service H
  • Android:ActionBar 在显示和隐藏时出现故障

    我想在单击时显示 隐藏操作栏 它确实显示和隐藏 但它并不平滑 底部隐藏但在消失之前有一段时间不同的背景 我什至在一个简单的 hello world 应用程序中尝试过 结果是相同的 这是代码 public void onCreate Bund
  • os.close(0) 和 sys.stdin.close() 之间的区别

    我正在编写一些 Python 代码 它是从 Apache 调用的 CGI 脚本 代码所做的第一件事是 我相信 尝试使用以下命令关闭 stdin stdout stderr for fd in 0 1 2 try os close fd ex
  • Websphere 由于 c3p0 挂起

    我正在使用 WAS 7 1 以及 c3p0 v 0 9 2 1 和 hibernate 3 2 6ga 使用几个小时后 Websphere 挂起 我在日志中看到此消息 6 24 13 10 57 50 377 CEST 00000031 T
  • React JS - 使用后退按钮获取以前的搜索

    因此 我编写了一个小型单页应用程序 它查询 API 并返回一堆结果 我将这些结果显示给用户在输入搜索词的输入字段下方 每次我输入新的搜索词并按 Enter 键时 都会重新查询 API 并使用新结果更新页面 但是 我希望能够单击浏览器中的后退
  • 无法向 FB 提交错误报告

    浏览 FB bug 后https developers facebook com bugs https developers facebook com bugs 并且没有找到我的问题的答案 我在 SO 上发布了一个问题 https stac
  • 如何在 C# 中检查数据库(ACCESS 或 SQL)中是否存在表

    我发现很多关于这个问题的问题 但是有没有什么简单的语句可以完成这个任务呢 对于 SQL 和 ACCESS IF EXISTS SELECT 1 FROM sys tables WHERE name table name BEGIN do s
  • 使用 SharedPreferences 保存多个 EditText 值

    我正在尝试构建一个应用程序 人们可以在其中填写个人数据 例如姓名 电话号码 电子邮件 对于上面提到的每个字段 我创建了一个EditText 现在我的目标是使用保存用户的输入SharedPreferences这样他 她就不必每次重新打开应用程
  • Docker 容器无法连接到远程 MongoDB

    我有一个flask基于 python 代码 只需连接到mongodb 有两条路线Get Post Get简单地打印hello world并使用Post我们可以发布任何稍后保存的 json 数据MongoDB这段 python 代码运行良好
  • 方法声明后的冒号?

    public function getRecords int id array 你好 有人能告诉我在 PHP 接口内的这个方法声明中冒号在这里做什么吗 这是 PHP 7 语法吗 数组在这里的含义是什么 方法必须返回数组还是其他东西 是的 这
  • GitHub 在存储库上运行 git-gc 的频率如何?

    假设 GitHub 偶尔运行git gc https git scm com docs git gc这种情况发生的频率如何 GitHub 支持回答了这个问题 https twitter com githubhelp status 38792
  • 在Python中进行函数链接时,有没有办法引用“当前”对象?

    假设我接受了以下课程 class foo object def init self int self count int def increment self int return foo int 1 def decrement self
  • 自定义日历视图

    I want to develop custom calendarView for my app i think i can make it by use of UICollectionView like this 我尝试了这个简单的事情