重新加载UIViewController

时间:2018-01-22 10:03:43

标签: swift uitableview

我学得很快,今天我遇到了问题。我正在创建一个显示电影院及其日程安排的应用程序。像这样:

enter image description here

这个想法是有3个按钮,可以显示今天,第二天和第二天的剧院日程安排(我不知道正确的单词,抱歉:()如果点击的话。例如,第一个按钮显示今天的日程安排:

enter image description here

第一次加载视图时,它始终显示今天的日程安排。但其他按钮不起作用,我不知道为什么。

我在UIViewController viewDidLoad方法中获得了tableView的委托和数据源。

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()

    // Delegate/datasource for tableView that shows theater's schedule
    showtimeTable.delegate = self
    showtimeTable.dataSource = self
    showtimeTable.tableFooterView = UIView()

这是动作功能。因为3个按钮具有相同的目的,所以我将它们全部连接到1个func,我进行了测试,并且它们与"获取按钮文本"方法

enter image description here

@IBAction func isClickedDate(_ sender: UIButton) {
    selectedDay = Int(sender.titleLabel!.text!)!
}

选择一天后,应用程序将抛出所选剧院的所有房间和每个房间的所有节目,并选择与selectedDay当天相同的每个节目,将它们存储在列表中并返回到全局变量showOnDate:[显示]

extension ShowtimeViewController: UITableViewDelegate, UITableViewDataSource {

// MARK: Showtime tableView lifecycle and settings

// Set number of section based on number of show performed on selected day
func numberOfSections(in tableView: UITableView) -> Int {
    return showOnDate.count
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "showtimeCell", for: indexPath) as! ShowtimeTableViewCell
    return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let cell = cell as! ShowtimeTableViewCell
    let dateTimeComponents = Calendar.current.dateComponents(dateComponents, from: showOnDate[indexPath.section].sDate)

    // Set cell image and labels based on shows of selected theater
    cell.showImage.image = showOnDate[indexPath.section].sFilm.fPoster
    cell.showName.text = "\(showOnDate[indexPath.section].sFilm.fName) (\(showOnDate[indexPath.section].sFilm.fYear))"
    cell.showTime.text = "\(dateTimeComponents.hour!):\(dateTimeComponents.minute!)"
}

// Get shows will be performed on selected date
func getShowList(date: Int) -> [Show] {
    var listShow = [Show]()
    for room in selectedTheater!.tRooms {
        for show in room.rShow {
            let dateTimeComponents = Calendar.current.dateComponents(dateComponents, from: show.sDate)
            if date == dateTimeComponents.day! {
                listShow += [show]
            }
        }
    }
    return listShow
}

}

我试图将showtimeTable.reloadData()包含在动作功能中但没有任何反应。尝试DispatchQueue.main.asyncbeginUpdates()endUpdates(),没有任何效果(我尝试了所有我知道的事情,也许它甚至没有任何影响)。现在我哭了,我需要帮助。非常感谢! (我没有足够的声誉来嵌入图片,抱歉:()

2 个答案:

答案 0 :(得分:1)

在按钮操作中,您需要使用与showOneDate相关的节目更新selectedDay数组(如viewDidLoad中所示),然后重新加载表格视图

@IBAction func isClickedDate(_ sender: UIButton) {
    selectedDay = Int(sender.titleLabel!.text!)!
    showOnDate = getShowList(date: selectedDay)
    tableView.reloadData()
}

将(更多)代码发布为文本的原因是愿意提供帮助的人不一定愿意重新键入所有代码

建议:

不是从titleLabel获取整数,而是将相应的标签分配给按钮,然后您只需编写selectedDay = sender.tag

答案 1 :(得分:0)

除了更改showOnDate变量的值

之外,与按钮关联的操作无效

所以,我的问题是:你以哪种方式检索要在表格视图中显示的元素?即,您能展示数据源方法的实现吗?

在这里,您可以通过不同的方式实现它。一个简单的方法是分配给reloadData变量,基于该选择的新列表,然后触发@IBAction func isClickedDate(_ sender: UIButton) { selectedDay = Int(sender.titleLabel!.text!)! showOnDate = getShowList(selectedDay) tableView.reloadData() } 方法。 如果您已经在主线程上运行,则不需要对main执行任何调度。

样品:

Int(sender.titleLabel!.text!)!

注意:

我会避免使用此代码

tag

因为非常难看。也许(仅用于学习新事物)您可以使用button.tag = 22 button2.tag = 23 // etc 属性来实现此目的。代码将变得更加干净。

@IBAction func isClickedDate(_ sender: UIButton) {
    showOnDate = getShowList(sender.tag)
    tableView.reloadData()
}

{{1}}