【问题标题】:How to display UITableView according to day selected?如何根据选择的日期显示 UITableView?
【发布时间】:2016-12-29 05:36:08
【问题描述】:

我有一个带有 2 个按钮的表格视图,一个按钮用于 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 时,我喜欢在表格视图中看到Dog and Rabbit,因为今天是Thursday = 5,我按yestredayBtn,我喜欢看到Cat and Dog,因为它是Wednesday = 4

我遇到了这个扩展来寻找工作日的价值,但不知道如何将它应用为按钮标签

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

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

【问题讨论】:

  • 您可以在 viewDidLoad 中设置按钮的标签,然后在按钮操作上设置适当的数组并重新加载 tableview

标签: ios swift uitableview uibutton


【解决方案1】:

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

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
    }
}

【讨论】:

  • 感谢您的回复,但当我同时按下 todayBtn 和昨天 Btn 时,我得到相同的结果 Cat,Cat, Dog, Dog, Rabbit, Rabbit。
  • func actChangeData(_ sender: UIButton) 更新了工作代码!
  • 这行得通,但无论如何我只能显示那天的数据我的意思是当我按今天Btn 时,我喜欢看狗和兔子,我按昨天Btn,我喜欢看猫和狗。但是现在每次我按下按钮时数据都会重复。
  • 是的,在每次过滤之前使用arrdata.removeAll() 更新代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 2014-01-09
  • 2019-03-05
  • 1970-01-01
相关资源
最近更新 更多