ios - 如何根据选择的日期显示 UITableView?

标签 ios swift uitableview uibutton

我有一个带有 2 个按钮的 tableview,一个按钮用于 today,第二个按钮用于 yesterday,如下所示以及以下数据,其中 tDays 的整数数组表示值像 Sunday = 1,....Saturday = 7

这样的工作日
var tAnimals:[String] = ["Cat", "Dog", "Rabbit"]
var tDays:[[Int]] = [[3,4],[4,5],[5,6]]

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var todayBtn: UIButton!
@IBOutlet weak var yesterdayBtn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    myCell.titleLabel.text = "Animals"
    return myCell
}
}

现在,当我按下 todayBtn 时,我喜欢在表格 View 中看到 Dog and Rabbit,因为今天是 Thursday = 5,然后我按下yestredayBtn,我喜欢看 猫狗 因为它是 星期三 = 4

我发现这个扩展是为了寻找工作日的值(value),但不知道如何将它应用为按钮标签

extension Date {
    func dayNumberOfWeek() -> Int? {
        return Calendar.current.dateComponents([.weekday], from: self).weekday
    }
}

有没有一种方法可以将工作日的值分配为按钮标签,以便它们每天相应地自动更新并显示正确的数据。

最佳答案

可能是这样的,可能需要一些折射,我希望你能明白基本的想法:

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var tAnimals:[String] = ["Cat", "Dog", "Rabbit"]
    var tDays:[[Int]] = [[3,4],[4,5],[5,6]]
    var arrdata:[String] = []
    var today:Int!
    var day:Int!

    @IBOutlet weak var myTableView: UITableView!
    @IBOutlet weak var todayBtn: UIButton!
    @IBOutlet weak var yesterdayBtn: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

        today = Date().dayNumberOfWeek()
    }

    //connect this IBAction with todayBtn & yesterdayBtn
    @IBAction func actChangeData(_ sender: UIButton) {

        arrdata.removeAll()
        if sender == todayBtn {
            day = today
        } else {
            day = today - 1
        }

        for i in 0...tAnimals.count-1 {
           if tDays[i].contains(day) {
               arrdata.append(tAnimals[i])
            }
        }

        myTableView.reloadData()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        myCell.titleLabel.text = arrdata[indexPath.row]
        return myCell
    }
}

extension Date {
    func dayNumberOfWeek() -> Int? {
        return Calendar.current.dateComponents([.weekday], from: self).weekday
    }
}

关于ios - 如何根据选择的日期显示 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373683/

相关文章:

ios - 如何从 Apple 获取自动续订订阅价格

ios - Objective-C . 追踪无法识别的选择器

ios - 以编程方式删除 subview

ios - 向 UITextView 添加阴影会使文本扩展到 UITextViewFrame 之外

ios - Firebase Crashlytics 无法激活

ios - Swift 自定义 TableViewCell(卡)

ios - 如何快速格式化用户显示(如社交网络)的时间间隔?

ios - UICollectionViewCell 约束问题

ios - Swift 2 - 无法在单独的 ViewController 中加载 UIWebView

ios - MvvmCross 中的 CoreAnimation 和 ObservableCollection