ios - 如何在 UITableView 中删除带有计时器的自定义单元格?

标签 ios swift uitableview timer

我正在开发一个应用程序,它有一个“加号”按钮,可以将秒表添加到表格 View ,每个单元格都有自己的计时器,并且可以自己播放。

当我尝试像那样删除一个单元格时,会发生如下随机问题:

  1. 正在更改的秒表顺序
  2. 一些秒表的时间正在归零。
  3. 如果之后尝试添加新的秒表,带有计时器的旧秒表又回来了!

表格 View

class StopWatchViewController: UIViewController {
    @IBOutlet weak var stopWatchesTableView: UITableView!
    var stopwatchesList: [String] = []

    var stopwatchesNum : Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        stopWatchesTableView.delegate = self
        stopWatchesTableView.dataSource = self

        NotificationCenter.default.addObserver(self, 
                                               selector: #selector(applicationDidEnterBackground(noti:)),                        
                                               name: UIApplication.didEnterBackgroundNotification,
                                               object: nil)
    }

    @objc func applicationDidEnterBackground(noti: Notification) {
        // Save Date
        let shared  = UserDefaults.standard
        shared.set(Date(), forKey: "SavedTime")
        print(Date())
    }

    func refresh() {
        stopWatchesTableView.reloadData()
    }

    @IBAction func AddStopWatch(_ sender: Any) {
        stopwatchesNum += 1;
        stopwatchesList.append(String(format: "Stopwatch %d", stopwatchesNum))
        refresh()
    }
}

extension StopWatchViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return stopwatchesList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let stopWatch = stopwatchesList[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "StopwatchCell") as! StopWatchCell
        cell.initCell(title: stopWatch, index: indexPath.row)
        return cell
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete {
            stopwatchesList.remove(at: indexPath.row)
            stopWatchesTableView.deleteRows(at: [indexPath], with: .automatic)
            refresh()
        }
    }
}

什么会导致此类问题?

最佳答案

删除行后不要调用刷新方法。希望对您有所帮助。

关于ios - 如何在 UITableView 中删除带有计时器的自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361119/

相关文章:

iphone - 无法识别的选择器 -[UIView addSubView :]

ios - iOS 8 和之前版本中自动旋转的区别

swift - 快速动画摇摆

ios - 如何设置视频录制10秒限制并实时删除10秒旧电影部分 - Swift iOS

ios - 如何触发 Collection View 的 didSelectItemAtIndexPath : on tap while it's scrolling?

ios - 低级蓝牙数据包分析

ios - 是否可以从应用程序内部监控来自 iOS 应用程序的所有网络流量?

ios - 突出显示所选的tableViewCell以与UITextfield.text匹配

ios - UITableViewDataSource 和 UIViewController 之间的通信

ios - 如何删除出现在 uitableview 下方的空白