在独立的时间间隔内重新加载选定的收集单元

时间:2019-03-15 01:48:55

标签: ios swift timer uicollectionview uicollectionviewcell

我正在尝试使用collectionView制作倒数计时器应用程序。

特征与功能:

  • 每个单元格都有自己的标签和计时器功能。
  • 如果用户触摸某个单元格,倒数计时器将运行。
  • 必须在计时器运行时更​​新时间字符串。

我在每个单元格中成功构建了一个计时器,但我一直无法更新timeLabel(重新加载选定的单元格)。

请检查下面的代码,并给我一些提示。

class ListViewController: UIViewController {

@IBOutlet weak var collectionView: UICollectionView!


var recipeList: TimeRecipeList
var timer = Timer()

required init?(coder aDecoder: NSCoder) {
    recipeList = TimeRecipeList()
    super.init(coder: aDecoder)
}



override func viewDidLoad() {
    super.viewDidLoad()
    let width = (view.frame.size.width - 10) / 2
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = CGSize(width: width, height: width)

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "AddItemSegue" {
        if let addItemVC = segue.destination as? AddRecipeViewController {
            addItemVC.delegate = self
        }
    }
}

}

extension ListViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return recipeList.item.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecipeListCell", for: indexPath)
        let item = recipeList.item[indexPath.row]
        configureText(for: cell, with: item)
        return cell
    }

    func configureText(for cell: UICollectionViewCell, with item: TimeRecipe) {
        if let label = cell.viewWithTag(1) as? UILabel {
            label.text = item.name
        }

        if let label = cell.viewWithTag(2) as? UILabel {
            let formatter = DateComponentsFormatter()
            formatter.unitsStyle = .abbreviated
            let timeString = formatter.string(from: TimeInterval(item.time))
            label.text = timeString
        }
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecipeListCell", for: indexPath)
        let item = recipeList.item[indexPath.row]

        let cellTimer = TimerControl()
        cellTimer.second = item.time
        cellTimer.timerRun()

        configureText(for: cell, with: item)

    }


    class TimerControl {

        var timer = Timer()
        var second: Int = 0


        func timerRun() {
            timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDown), userInfo: nil, repeats: true)
        }

        @objc func countDown() {
            //let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecipeListCell", for: indexPath)

                if second <= 1 {
                    timer.invalidate()
                } else {
                    second -= 1
                    //collectionView.reloadItems(at: [indexPath])

                    //MARK: Reload selected cell
               }
        }

    }


}

extension ListViewController: AddRecipeViewControllerDelegate {
    func addRecipeViewControllerDidCancel(_ controller: AddRecipeViewController) {
        dismiss(animated: true)
    }

    func addRecipeViewControllerDisSave(_ controller: AddRecipeViewController, didFinishAdding item: TimeRecipe) {
        dismiss(animated: true)
        let rowIndex = recipeList.item.count
        recipeList.item.append(item)

        let indexPath = IndexPath(row: rowIndex, section: 0)
        let indexPaths = [indexPath]
        collectionView.insertItems(at: indexPaths)
    }


}

2 个答案:

答案 0 :(得分:-1)

在计时器控件类中传递 indexpath 。当计时器完成后,根据传递索引路径重新加载收集单元格

答案 1 :(得分:-1)

@objc func countDown(indexPath: IndexPath) {
        if second <= 1 {
            timer.invalidate()
        } else {
            second -= 1
            //MARK: Reload selected cell
            yourarray[indexPath.row] = newvalues
            collectionView.reloadItems(at: [indexPath])
        }
    }