【问题标题】:Swift clear tableview data after pressing close button按下关闭按钮后迅速清除表格视图数据
【发布时间】:2017-04-19 13:36:59
【问题描述】:

我想问一下,我按下关闭按钮后如何清除表格视图数据?正如我在以前的解决方案中所理解的,是在tableview.reloadData() 之前使用removeall() 函数。但是,我的reloaddata 是在覆盖主,因为如果我removeall,在我进入我的表格视图后,所有数据都将丢失。

 override func viewDidLoad() {
        super.viewDidLoad()
        print("popupname",name2_2)
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

        self.showAnimate()
        tableView.dataSource = self
        tableView.delegate = self
    }

    @IBAction func close(_ sender: Any) {
 name2_2.removeAll()
        tableView.reloadData()

        self.removeAnimate()
    }

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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedrow = [name2_2[indexPath.row]]
        self.performSegue(withIdentifier: "unwindToMain1", sender: self)
        self.removeAnimate()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath as IndexPath)

        cell.textLabel?.text = name2_2[indexPath.row] as String

        return cell
    }

    func showAnimate()
    {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0;
        UIView.animate(withDuration: 0.25, animations: {
            self.view.alpha = 1.0
            self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        });
    }

    func removeAnimate()
    {
        UIView.animate(withDuration: 0.25, animations: {
            self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
            self.view.alpha = 0.0;
        }, completion:{(finished : Bool)  in
            if (finished)
            {
                self.view.removeFromSuperview()
            }
        });
    }

我需要关闭功能,请指导我我真的需要帮助。

【问题讨论】:

  • 您的问题不清楚。 “但是,我的 reloaddata 位于 override main” 是什么意思?要清除表,请更新数据模型使其没有数据,然后使用reloadData 重新加载表视图。你遇到了什么问题?
  • 这就是我在 override func viewdidload 清除数据时的问题,进入后数据全部消失。我想要的是在按下关闭按钮后清除 tableview 数据
  • 然后清除数据模型并在您的close 方法中调用reloadData,而不是在viewDidLoad 中。
  • 呃,这就是我进入视图控制器后的问题,我还需要重新加载表视图数据,这样我才能看到数据列表,那么我该怎么做呢?我需要放入 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
  • Edit 您的问题以及表视图数据源方法的相关代码。并通过尝试清除和重新加载表格视图来更新您的代码。

标签: ios swift uitableview


【解决方案1】:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 button.isSelected?0:name2_2.count };

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多