使用Tabbar时表格视图不会重新加载?

时间:2018-03-06 05:23:27

标签: ios swift uitableview core-data

我正在使用标签栏。 当我选择第一个标签栏图标时,这里我们将添加一些任务并保存到coredata对象中。 然后,我选择了第二个标签栏图标,此处表格视图不会重新加载数据。 当我点击第二个视图控制器中的任何按钮并转到另一个视图控制器屏幕,然后弹出视图控制器或返回到该屏幕。然后表格视图显示我添加的数据。

为什么在添加任务并转到第二个视图控制器时,表视图不会重新加载数据?

Constant.swift

 var tasks: [Task] = []
 var filterTodayTask: [Task] = []

CoreData.swift

 class CoreData {
        static func filterTodayTask(filteredObject: String) -> [Task]
            {
                let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest()

                var user: [Task]? = nil

                let predicate = NSPredicate(format: "date_from == %@", filteredObject)
                fetchRequest.predicate = predicate

                do {
                    user = try context.fetch(fetchRequest)
                    return user!
                }
                catch {
                    return user!
                }
            }
}

ViewController.swift

@IBAction func btnCreate(_ sender: Any)
    {

        let alert = UIAlertController(title: "Alert", message: "Are you sure you want to save this task?", preferredStyle: .alert)
        let actionOK = UIAlertAction(title: "OK", style: .default) { (action) in

            //Function used to save the details in Core Data Models
            CoreData.saveData(tfCategory: self.tfCat, tfTask: self.tfTask, tfDateFrom: self.tfDateFrom, tfDateTo: self.tfDateTo, tfTimeFrom: self.tfTimeFrom, tfTimeTo: self.tfTimeTo, tvDesc: self.tvDesc, tfTag: self.tfTag, tfReminder: self.tfReminder)


        }

        let actionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

        alert.addAction(actionCancel)
        alert.addAction(actionOK)
        self.present(alert, animated: true, completion: nil)

    }

secondViewController.swift

    override func viewWillAppear(_ animated: Bool)
        {
            super.viewWillAppear(true)

            tblToday.reloadData()
            tblTomorrow.reloadData()
        }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        if tableView == self.tblToday
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TodayTaskCell", for: indexPath) as! TodayTaskTVCell
            cell.tfTime.isEnabled = false
            cell.tfRepeat.isEnabled = false
            //cell.lblColor.backgroundColor = strColor

            if isFiltered
            {
                print("Entered Today Button")

                TextFieldCalender.imgWithinRepeat(textField: cell.tfRepeat)
                TextFieldCalender.imgWithinTime(textField: cell.tfTime)

                let filteredResult = filteredTask[indexPath.row]
                cell.lblTask.text = filteredResult.task_name
                cell.tfTime.text = filteredResult.time_from

                return cell
            }
            else if isToday
            {
                print("Entered Today Button")

                TextFieldCalender.imgWithinRepeat(textField: cell.tfRepeat)
                TextFieldCalender.imgWithinTime(textField: cell.tfTime)

                let filteredResult = filterTodayTask[indexPath.row]
                cell.lblTask.text = filteredResult.task_name
                cell.tfTime.text = filteredResult.time_from

                return cell
            }
            else {
                //This can execute every time when the view is loaded for the first time.
                print("Entered Today Button")

                TextFieldCalender.imgWithinRepeat(textField: cell.tfRepeat)
                TextFieldCalender.imgWithinTime(textField: cell.tfTime)

                let filteredResult = filterTodayTask[indexPath.row]
                cell.lblTask.text = filteredResult.task_name
                cell.tfTime.text = filteredResult.time_from

                return cell
            }
        }
}

0 个答案:

没有答案