简体   繁体   English

您如何在快速ios中设计“周视图”?

[英]How would you design a “week view” in swift ios?

In the photo above the UI details a design with a 7-day collection view (a "week view") that auto-increments each cell to the next day when the day passes, this is because the 7 cells are static. UI上方的照片中详细说明了具有7天收集视图(“周视图”)的设计,该视图在一天过去时自动将每个单元格增加到第二天,这是因为这7个单元格是静态的。 Each day has a list of tasks associated with it, as seen above, each day the user must try and complete all the assigned tasks by their personal trainer. 每天都有与之相关的任务列表,如上所示,每天用户必须尝试由其私人教练完成所有分配的任务。

I have listed a couple of design questions below on how to go about building an app like this: 我在下面列出了一些设计问题,涉及如何构建这样的应用程序:

How would I design the 7 collection cells to auto-update as a new day passes? 我将如何设计这7个收集单元以在新的一天过去时自动更新? How can I have each day hold their own unique list of tasks? 我怎样才能每天拥有自己独特的任务清单? I'll provide some data model code below and my ideas alongside the code. 我将在下面提供一些数据模型代码以及我的想法和代码。

Here is a struct for my tasks, I chose a struct because each day's tasks could be different, but if they happen to be similar or the same then copying the struct and modifying it from there would be more optimal I think. 这是我的任务的结构,我选择一个结构是因为每天的任务可能不同,但是如果它们碰巧相似或相同,则复制该结构并对其进行修改,我认为这将是最佳选择。 The tasks would be created by a personal trainer in another screen but I think this model is good enough. 这些任务将由私人教练在另一个屏幕上创建,但是我认为这种模型已经足够了。 What do you guys think? 你们有什么感想?

struct Task {
    var taskIcon: UIImage!
    var taskName: String!
    var taskDesc: String!
}

// Put the Task model in array to use in each day?
var tasks = [Task]()

For the "week view" how would I go about assigning a unique list of tasks for each day? 对于“周视图”,我将如何为每天分配唯一的任务列表? When the personal trainer creates the task for the day, how does the collection view index hold a unique array of tasks? 当私人教练创建当天的任务时,集合视图索引如何包含唯一的任务数组? I'm not sure if this would be the most optimal way of doing this 我不确定这是否是执行此操作的最佳方法

I'm not necessarily looking for anyone to write out code (although that'd be great if you did) but rather just an overview explanation of how it should be done. 我并不一定要寻找任何人来写出代码(尽管如果这样做的话那会很棒),而只是对应该如何做的概述进行解释。 Any advice is welcomed. 任何建议都欢迎。

IF you are always going to have 7 cells across the top of the view, and only 7 cells, I honestly wouldn't use a UICollectionView . 如果您总是在视图顶部有7个单元格,而只有7个单元格,那么老实说我不会使用UICollectionView UICollectionView s really shine when you have lots of data/images that you need to be able to scroll through. 当您有大量需要滚动浏览的数据/图像时, UICollectionView确实很UICollectionView Instead I would make those 'cells' normal UIView s(7) placed inside of a horizontal StackView. 相反,我将那些“单元”放置在水平StackView内的普通UIView s(7)。

You could then give those UIView's a target action for the tapped gesture, which updates and changes your DatSource used to fill in your UITableView at the bottom with the needed tasks. 然后,您可以为那些UIView提供轻击手势的目标动作,该动作将更新和更改DatSource,该DatSource用于在底部用所需的任务填充UITableView。

As far as updating with the date, making a call to Date() will give the device's current dateTime stamp. 至于更新日期,调用Date()会给出设备的当前dateTime时间戳。 You could use that Date object's `.addingTimeInterval()' to add 24,48, etc... hours for the following days. 您可以使用该Date对象的`.addingTimeInterval()'在接下来的几天添加24,48等小时。

EDIT: 编辑:

Sure for an example, you mention you are going to have your tasks in an array, You could create a Dictionary to hold an array of task for each day, or a array of arrays: var taskDictionary : [String : [Task]]? 可以肯定地举一个例子,您提到您将要在一个数组中放置任务,您可以创建一个Dictionary来保存每天的任务数组或数组数组: var taskDictionary : [String : [Task]]? or var taskArray : [[Task]]? 还是var taskArray : [[Task]]?

Set the tapGesture's selector to a function that will control pulling content from the Dictionary/Array: 将tapGesture的选择器设置为将控制从Dictionary / Array提取内容的功能:

func dayOfWeekTapped(_ sender: UIView) {
    myTableViewDataSource = taskDictionary[sender.tag]
    //myTableViewDataSource = taskArray[sender.tag]
    myTableView.reloadData()
}

I used tag property here for an example, but it would be better to create a UIView subclass, so that you could access the UIElements inside the view: 我在这里使用tag属性作为示例,但是最好创建一个UIView子类,以便可以在视图内部访问UIElements:

func dayOfWeekTapped(_ sender: CustomUIView) {
    myTableViewDataSource = taskDictionary[sender.dateLbl.text]
    //myTableViewDataSource = taskArray[(sender.dateLabel.text as! NSString).intValue]
    myTableView.reloadData()
}

I was thinking about this question and got curious, but the Custom UIView could look something like this(very quickly thrown together so it could be cleaned up a bit): 我在考虑这个问题并感到好奇,但是Custom UIView可能看起来像这样(很快就被扔到了一起,因此可以进行清理):

protocol DayViewTappedDelegate {
    func daySelected(_ selectedLabelText: String)
}

class DayView: UIView {
    @IBOutlet var dateLbl: UILabel!

    var tapGesture: UITapGestureRecognizer?
    var delegate: DayViewTappedDelegate?

    func setDelegateAndTap(_ delegate: DayViewTappedDelegate) {
        self.delegate = delegate
        self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dayViewTapped))
        tapGesture?.numberOfTapsRequired = 1

        self.addGestureRecognizer(tapGesture!)
    }

    @objc
    func dayViewTapped() {
        delegate!.daySelected(self.dateLbl.text!)
    }
}

VC: VC:

class ViewController: UIViewController, DayViewTappedDelegate {
    @IBOutlet var dayBtns : [DayView]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        for i in 0 ..< dayBtns.count {
            let date = Date().addingTimeInterval(TimeInterval(86400 * i))
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd"

            dayBtns[i].dateLbl.text = dateFormatter.string(from: date)
            dayBtns[i].dateLbl.textAlignment = .center
            dayBtns[i].setDelegateAndTap(self)
        }
    }

    func daySelected(_ selectedLabelText: String) {
        print(selectedLabelText)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM