我的计时器标签在滚动后占用一个新数字

问题描述 投票:-1回答:1

滚动或重新加载qazxsw poi后,我的计时器标签占用一个新数字,同时它每秒显示几个数字。任何人都可以帮我想要我的标签秀吗?

我希望我的标签只显示它最初出现的数字

在您看到的图片中,在滚动或中继表之后,计时器每秒显示几个值/秒。我把我的代码和图片

tableview

和我在tableview单元格中的代码:

import Alamofire
import Foundation

class Games: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var gamesTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        Alamofire.request("http://188.158.121.200:3030/shans.com/get_main_room_list", method:.post, headers: headers).responseJSON { (newresponse) in
            do {
                let decoder = JSONDecoder()
                let response = try decoder.decode(Root1.self, from: newresponse.data!)
                switch(response.joinedUser) {
                case  .joinedUserElementArray( let arr):
                    AppDelegate.AllofGames_Ids = arr.map { $0.id }
                case .string( _) :
                    break
                }
                if AppDelegate.AllofGames.isEmpty == true {
                    for each in response.allRooms {
                        let epocTime = TimeInterval(Int(each.matchTime)!) / 1000
                        if each.finished == true || AppDelegate.AllofGames_Ids.contains(each.id)  {
                            //Do nothing
                        }else {
                            let structGames = StructAllofGames(finished: each.finished, pic: each.pic, _id: each.id, coininput: each.coinInput, Award_money: each.awardMoney, Award_coin: each.awardCoin, Match_time: Int(epocTime), Winner_number: each.winnerNumber, Free: each.capacity.free, Total: each.capacity.total)
                            AppDelegate.AllofGames.append(structGames)
                            DispatchQueue.main.async{
                                self.gamesTableView.reloadData()
                            }//dispatch
                        }//else
                    }
                } else {
                    print("fk")
                }
            }
        }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let Cell =  tableView.dequeueReusableCell(withIdentifier: "tableview1", for: indexPath) as! MyCustomCell

        Cell.SekeVorudi.text = String(AppDelegate.AllofGames[indexPath.row].coininput)
        Cell.jaize.text =  String(AppDelegate.AllofGames[indexPath.row].Award_money) + "تومان"
        Cell.ZarfiatOtagh.text = String(AppDelegate.AllofGames[indexPath.row].Total) + "/" + String(AppDelegate.AllofGames[indexPath.row].Free)
        Cell.TedadBarande.text = String(AppDelegate.AllofGames[indexPath.row].Winner_number)
        let  Fulllink    = AppDelegate.AllofGames[indexPath.row].pic
        Cell.imageview.downloaded(from: Fulllink)
        let (h,m,s)  = secondsToHoursMinutesSeconds(seconds: AppDelegate.AllofGames[indexPath.row].Match_time)
        let HoursToMinute = h * 60
        let AllOfMinutes = HoursToMinute + m
        Cell.timer(m: AllOfMinutes, s: 0)
        return Cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

}
swift uitableview timer tableview countdowntimer
1个回答
1
投票

class MyCustomCell: UITableViewCell { @IBOutlet weak var timelabel: UILabel! @IBOutlet weak var SekeVorudi: UILabel! @IBOutlet weak var jaize: UILabel! @IBOutlet weak var TedadBarande: UILabel! @IBOutlet weak var ZarfiatOtagh: UILabel! @IBOutlet weak var imageview: UIImageView! override func awakeFromNib() { super.awakeFromNib() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func timer(m: Int, s: Int) { DispatchQueue.global(qos: .userInteractive).async { var hour : Int = m/60 var minute : Int = m % 60 var second : Int = s DispatchQueue.main.async { self.timelabel.text = (String(format: "%02d:%02d:%02d", hour, minute, second)) } while true { sleep(1) if hour == 0 && minute == 0 && second == 0 { DispatchQueue.main.async { UIView.animate(withDuration: 1, animations: { self.timelabel.alpha = 0 }) } } else if second == 0 { if minute != 0 { minute = minute - 1 second = 59 } else if second == 0 && minute == 0 { hour = hour - 1 minute = 59 second = 59 } } else if second != 0 { second = second - 1 } DispatchQueue.main.async { self.timelabel.text = (String(format: "%02d:%02d:%02d", hour, minute, second)) } } } } } 顶部设置一个可选变量,并将其值设置为您想要的单元格数,并在UIViewController中检查该值是否为CellForRowAt,如果没有将您的标签设置为该数字,如果它的nil正常保持正常做。

这个问题主要是由于当你滚动单元格出列时排队单元格从nil排队,因此调用UITableView以便重置默认数据,在你的情况下再次重置CellForRowAt值。

在你的情况下,我建议设置功能/单元格的定时器外侧,这样你就可以正常使用该值而无需重置。

© www.soinside.com 2019 - 2024. All rights reserved.