XCode Swift Timer在滚动时在tableview中重新加载
当我想在uitableview单元格中使用计时器时遇到问题。 每次当我滚动到底部,然后再次向上重置计数器的原始值。XCode Swift Timer在滚动时在tableview中重新加载
我知道问题是什么..当滚动回来时,单元格被重新加载。但我怎么能实现单元格不滚动上下滚动时,因此我可以保留原单元格重新加载。任何伪代码都会有所帮助。
我的代码看起来像这样。
FirstViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(hexString: "#bde8fb")
parseXhr(offset,limit:limit)
self.timer = NSTimer(timeInterval: 1.0, target: self, selector: #selector(FirstViewController.fireCellsUpdate), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// algemene cell gegevens
let cell = self.tv.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let separator = UIView(frame: CGRectMake(0, 0, cell.bounds.size.width, 20))
separator.backgroundColor = UIColor(hexString: "#bde8fb")
cell.contentView.addSubview(separator)
cell.photo.setBottomBorder("#bde8fb")
cell.timeInterval = self.timercounter[indexPath.row]
// load the image
cell.photo.contentMode = UIViewContentMode.ScaleAspectFit
cell.photo.kf_setImageWithURL(NSURL(string:"\(Config.image_dir)\(imageUrlArray[indexPath.row])"),placeholderImage: UIImage(named: Config.lazyLoadImage))
// set the product name
cell.veiling_title.text = productNameArray[indexPath.row]
cell.veiling_title.textAlignment = .Center
return cell
}
func fireCellsUpdate() {
let notification = NSNotification(name: "CustomCellUpdate", object: nil)
NSNotificationCenter.defaultCenter().postNotification(notification)
}
而且我CustomCell.swift看起来像这样
var timercounter = 0
@IBOutlet var veiling_title: UITextView!
@IBOutlet var photo: UIImageView!
@IBOutlet var veilingSeconds: UILabel!
var timeInterval: Int = 0 {
didSet {
self.veilingSeconds.text = "\(timeInterval)"
}
}
func updateUI() {
if self.timeInterval > 0 {
self.timeInterval -= 1
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(CustomCell.updateUI), name: "CustomCellUpdate", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
请尝试以下
在FirstViewController
在cellForRowAtIndexPath
前右结束return
行SERT
cell.callback = { (interval) in
self.timercounter[indexPath.row] = interval
}
在CustomCell
添加属性
var callback : ((Int) ->())?
,改变timeInterval
var timeInterval: Int = 0 {
didSet {
self.veilingSeconds.text = "\(timeInterval)"
callback?(timeInterval)
}
}
回调的好处是,它可以捕获timercounter
数组和索引更新值相应地,每当timeInterval
更改时,路径。
感谢您的回复,但仍然没有工作:(:(。我创建了一个与您的实现的要点,也许你看到了一些东西,我很好奇计时器如何不重新加载(现在花2天的时间来解决这个问题).... [FirstviewController(https://gist.github.com/ermst4r/51410cfb427809b18ae34b3d646d977d) [Customcell.swift(https://gist.github.com/ermst4r/fb29c4ec3265ea6192b8d951fd162500) – erwinnandpersad
也许问题位于别的地方,回调应该是有效的 – vadian
好吧,我终于搞定了!
我的问题是,我不得不在tableview中创建多个定时器。每当我向上或向下滚动计时器都会产生奇怪的结果(如秒数不正确,如重叠标签和错误时间)。我在FirstViewControllers
1定时器来所有秒追加在tableview中创造的2倍,让说GlobalTimerArray
2.And另一个计时器正在运行什么每隔秒递减GlobalTimerArray
然后我我的cellForRowAtIndexPath做这样的事:
if !self.loadedCells.contains(indexPath.row) {
self.loadedCells.append(indexPath.row)
loadSeconds(cell, indexPath: indexPath)
} else {
cell.timeInterval = self.saveSeconds[indexPath.row]
print(secondsToHoursMinutesSeconds(self.saveSeconds[indexPath.row]))
}
和我viewDidLoad中看起来是这样的:
override func viewDidLoad() {
self.view.backgroundColor = UIColor(hexString: "#bde8fb")
socket.connect()
self.timer = NSTimer(timeInterval: 1.0, target: self, selector: #selector(FirstViewController.fireCellsUpdate), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
parseXhr(offset,limit:limit)
self.secTimer = NSTimer(timeInterval: 1.0, target: self, selector: #selector(FirstViewController.decSeconds), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.secTimer, forMode: NSRunLoopCommonModes)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func decSeconds()
{
for index in 0..<self.saveSeconds.count {
self.saveSeconds[index] -= 1
}
}
我希望这可以帮助别人,对我来说是很难理解的,但也许是因为这是我的第一个应用程序是什么我写在斯威夫特:)
您需要更新模型('timercounter')在每次'timeInterval'改变以保持模型和视图同步时,在特定的索引路径上。 – vadian
谢谢!,你有我的代码片段吗?我必须在FirstViewController.swift中更新这个吗? – erwinnandpersad
在'cellForRowAtIndexPath'中,我将索引路径(该行足够)传递给单元格。然后你就可以直接从单元更新'timercounter'数组中的计数器。如果'timercounter'是值类型,不要忘记将更改的值分配回数组。 – vadian