UitableView not scrolling past the first cell - ios

So I have a UITableView and on top cell(1st cell) I have a timer on the cell.detailText. The timer displays but the tableview can’t scroll past the first cell because the cell text label is constantly being updated by this timer. What can I do to make the table view scroll properly without stopping the timer when the user scrolls?
Please help
Thanks in advance.

You don't need to keep calling reloadData(). You can do all of your timer work in the cell itself - you just need to remember to invalidate timers when you are done with them. I maintain an app that does something similar and use the following:
#IBOutlet weak var timeLeftLbl: UILabel!
var secondTimer: Timer!
var secondsLeft: Int = 0
func configureCell(item: PreviousAuctionItem) {
//Timers
secondsLeft = getSecondsLeft(endTime: item.endDate)
timeLeftLbl.text = secondsLeft
secondTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.decreaseTimeLeft), userInfo: nil, repeats: true)
}
#objc func decreaseTimeLeft() {
if secondsLeft > 0 {
secondsLeft -= 1
timeLeftLbl.text = secondsLeft.formatTimeAsString()
} else {
timeLeftLbl.text = ""
secondTimer?.invalidate()
}
}
override func prepareForReuse() {
secondTimer?.invalidate()
}
The getSeconds method is an API method I use to get how long an item has left.
Hope this helps!

Related

Update custom tableviewcell label through NSTimer every 1 second in Swift 3

I have a custom TableViewCell.
I want the timer Label in each cell to decrease every second. I have referred to this link. The only problem is I am unable to change the Label's text. If I print it in the console, the value is coming fine. How can I reload the TableView after changing cell's value from custom cell class?
I am new to custom TableView cells so please help.
I have referred to this link too.
class CustomCell: UITableViewCell {
#IBOutlet weak var timerLabel: UILabel!
var timer = Timer()
func updateRow(){
print("started timer")
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(CustomCell.decreaseTimer), userInfo: nil, repeats: true)
}
func decreaseTimer(){
let cell = self
let timerVal = cell.timerLabel.text
print("decrease "+timerVal!)
//how to reflect this timerVal value in the cell?
}}
You need to assign update time value to label .
func decreaseTimer(){
let cell = self
let timerVal = cell.timerLabel.text
print("decrease "+timerVal!)
//how to reflect this timerVal value in the cell?
cell.timerLabel.text = timerVal
}}

Swift 2 - Print New Value from Slider at Regular Interval

I am trying to print a value from a slider at regular intervals. But only print the value if it is different to that last printed. I also do not want to miss any of the output values from the slider.
To do this I have created an array and added an element to the start of that array if it is different to the one already at the start. I have then used a repeating NSTimer to regularly call a function that prints the last element in the array before removing it from the array.
What happens when I run the app is the NSTimer stops anything being printed for it's set time, but then all of the elements print at once and more than one of each print. I've tried messing about with lots of different things - this is the closest I have got to making it work.
If you need to know any more info let me know.
I really appreciate any help given, thanks very much.
var sliderArray: [Float] = []
var timer: NSTimer!
let step: Float = 1
#IBAction func sliderValueChanged(sender: AnyObject)
{
let roundedValue = round(slider.value / step) * step
slider.value = roundedValue
if sliderArray.first != slider.value
{
sliderArray.insert(slider.value, atIndex: 0)
}
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(sendSliderPosition), userInfo: nil, repeats: true)
}
func sendSliderPosition()
{
if sliderArray.count > 0
{
print(self.sliderArray.last)
sliderArray.removeLast()
}
}
I would suggest using CADisplayLink. A CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display. Which is perfect for your slider case.
This will also not trigger unnecessary call when the slider or the UI is at rest.
class C: UIViewController {
var displayLinkTimer: CADisplayLink?
#IBOutlet weak var slider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
displayLinkTimer = CADisplayLink(target: self, selector: #selector(printSliderValue))
let runLoop = NSRunLoop.mainRunLoop()
displayLinkTimer?.addToRunLoop(runLoop, forMode: runLoop.currentMode ?? NSDefaultRunLoopMode )
displayLinkTimer?.paused = true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
displayLinkTimer?.paused = true
}
deinit {
displayLinkTimer?.invalidate()
}
func printSliderValue()
{
let step: Float = 1
let roundedValue = round(slider.value / step) * step
slider.value = roundedValue
print(roundedValue)
}
}
The basic idea is this:
--> Every time the screen needs to redraw (this will happen at max around 60 frames per second taking into consideration this is fps rate), we get a chance to perform function.
--> to do so, we add the displayLink to the Run Loop. (Run lopp processes input/ refreshes UI and time slices)
--> NOTE This method wont be called if there is no redraw needed on the screen. This is not a timer per say that fires periodically. It fires when redraw is needed. In Sliders case, we want this to fire when we move slightest of the slider too.
For more info on how it actually works try it out and see the apple documentation. Make sure to invalidate before deinitializing the ViewController.
Figured out the answer, thank to everyone for the help and suggestions:
override func viewDidLoad()
{
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: #selector(sendSliderPosition), userInfo: nil, repeats: true)
}
#IBAction func sliderValueChanged(sender: AnyObject)
{
let step: Float = 1
let roundedValue = round(slider.value / step) * step
slider.value = roundedValue
if sliderArray.first != slider.value
{
sliderArray.insert(slider.value, atIndex: 0)
}
}
func sendSliderPosition()
{
if sliderArray.count > 1
{
let end1 = sliderArray.count-2
print(sliderArray[end1])
sliderArray.removeLast()
}
}
Explanation:
If the new slider value is different to the one already in the array then add it to the array at the start. Use an NSTimer to repeatedly call the sendSliderPosition function from viewDidLoad. The function will only be performed if there is more than one element in the array. If there is, print the element before the last one and remove the last. This always ensures that there is one element in the array so the function does not always run and that the element printed is the most recent one that hasn't already been printed.

UIProgressView not working as expected with NSTimer - Swift

I am using an NSTimer to let the user know the app is working. The progress bar is set up to last 3 seconds, but when running, it displays in a 'ticking' motion and it is not smooth like it should be. Is there anyway I can make it more smooth - I'm sure just a calculation error on my part.
Here is the code:
import UIKit
class LoadingScreen: UIViewController {
var time : Float = 0.0
var timer: NSTimer?
#IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// Do stuff
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)
} //close viewDidLoad
func setProgress() {
time += 0.1
progressView.progress = time / 3
if time >= 3 {
timer!.invalidate()
}
}
}
As per Apple iOS SDK docs you can achieve it with the use of next API:
func setProgress(_ progress: Float, animated animated: Bool)
It adjusts the current progress shown by the receiver, optionally animating the change.
Parameters:
progress - The new progress value.
animated - true if the change should be animated, false if the change should happen immediately.
More info on this:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIProgressView_Class/index.html#//apple_ref/occ/instm/UIProgressView/setProgress:animated:
So in your case you should do it like this:
func setProgress() {
time += 0.1
dispatch_async(dispatch_get_main_queue()) {
progressView.setProgress(time / 3, animated: true)
}
if time >= 3 {
timer!.invalidate()
}
}
Also please note that it is a good practice to perform UI updates on main thread, so I just dispatched progress update on main queue.
Hope it will help you.

How to prevent lag when changing button title and color?

I am trying to have a button change title and color every second but there is lag. What happens is that when it changes from lets say YELLOW to RED the button will show R.. or simply ... for a split second. It does not happen every time the button changes, only a few times. This the the code I have.
#IBOutlet var tapButton: UIButton!
var color:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.yellowColor(), UIColor.greenColor()]
var colorName:[String] = ["RED", "BLUE", "YELLOW", "GREEN"]
func game(){
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(SecondViewController.subtractTime), userInfo: nil, repeats: true)
}
func subtractTime(){
seconds -= 1
let num1 = Int(arc4random_uniform(4))
let num2 = Int(arc4random_uniform(4))
tapButton.titleLabel?.textColor = color[num1]
tapButton.setTitle(colorName[num2], forState: UIControlState.Normal)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
game()
}
I have changed the segues to Show Details, this seems to have fixed my problem. I also have the timers stop when I change view controllers.
I no longer get the lag that I was experiencing before.

Delete NSTimer/UITableViewCell in real time?

I have a UITableView in which I have numerous timers set on each UITableViewCell. Each timer starts from when a user creates a "post" on my application and should expire within 24 hours. However, I want it so that when all 24 hours is over, the UITableViewCell deletes itself in real time but I can't seem to figure out where or when I should be deleting the timer. I have a method that will constantly refresh the timer every second using NSTimer.scheduledTimerWithTimeInterval and it updates the timers on each UITableViewCell every second. However, I can't find a method or find how I can find if each timer inside each UITableViewCell is finished. Obviously I can find if the timer is finished in viewDidLoad but that is only called right when the view becomes active. Is there any method I am missing or anything I can use to find if a timer via the scheduledTimerWithTimeInterval method is finished, and if it is, to delete it? Here is my code below:
//I have a self.offers array declared in the beginning of my class whcih will act as the UITableView data source.
var offers = [Offer]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Dequeue a "reusable" cell
let cell = tableView.dequeueReusableCellWithIdentifier(offerCellIdentifier) as! OfferCell
setCellContents(cell, indexPath: indexPath)
return cell
}
func setCellContents(cell:OfferCell, indexPath: NSIndexPath!){
let item = self.offers[indexPath.row]
cell.offerName.text = item.offerName()
cell.offerPoster.text = item.offerPoster()
var expirDate: NSTimeInterval = item.dateExpired()!.doubleValue
//Get current time and subtract it by the predicted expiration date of the cell. Subtract them to get the countdown timer.
var timeUntilEnd = expirDate - NSDate().timeIntervalSince1970
if timeUntilEnd <= 0 {
//Here is where I want to delete the countdown timer but it gets difficult to do so when you are also inserting UITableViewCells and deleting them at the same time.
self.offers.removeAtIndex(indexPath!.row)
self.offersReference = Firebase(url:"<Database Link>")
self.offersReference.removeValue()
self.tableView.reloadData()
cell.timeLeft.text = "Finished."
}
else{
//Display the time left
var seconds = timeUntilEnd % 60
var minutes = (timeUntilEnd / 60) % 60
var hours = timeUntilEnd / 3600
cell.timeLeft.text = NSString(format: "%dh %dm %ds", Int(hours), Int(minutes), Int(seconds)) as String
}
}
override func viewDidLoad() {
super.viewDidLoad()
var timeExpired = false
//I set up my offers array above with completion handler
setupOffers { (result, offer) -> Void in
if(result == true){
//Insert each row one by one.
var currentCount = self.offers.count
var indexPaths: [NSIndexPath] = [NSIndexPath]()
indexPaths.append(NSIndexPath(forRow:currentCount, inSection: 0))
self.offers.append(offer)
currentCount++
self.tableView.reloadData()
}
}
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.rowHeight = 145.0
}
//Called when you click on the tab
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.refreshTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "refreshView:", userInfo: nil, repeats: true)
//Should fire while scrolling, so we need to add the timer manually:
//var currentRunLoop = NSRunLoop()
//currentRunLoop.addTimer(refreshTimer, forMode: NSRunLoopCommonModes)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
refreshTimer.invalidate()
refreshTimer = nil
}
//Constantly refreshes the data in the offers array so that the time will continuously be updating every second on the screen.
func refreshView(timer: NSTimer){
self.tableView.reloadData()
}
You have a number of misconceptions, a few problems with your code, and your description isn't clear.
If you create a timer using scheduledTimerWithTimeInterval, you don't need to, and shouldn't, add it to the runloop. The scheduledTimerWithTimeInterval method does that for you.
If you create a repeating timer, it never "finishes." It keeps repeating forever. If instead you create a non-repeating timer, it fires once and then goes away. You don't need to delete it. Just don't keep a strong reference to it and it will be deallocated once it fires.
You say you create a timer for each table view cell, but the code you posted only creates a single timer for the view controller. You say "...it updates the timers on each UITableViewCell every second. However, I can't find a method or find how I can find if each timer inside each UITableViewCell is finished." That doesn't match the code you posted. Your code is running a timer once a second that simply tells the table view to reload it's data.
I guess you mean that you re-display a remaining time counter in each cell when your timer fires?
So, are you asking how to figure out if the time remaining for all of your cell's item.timeLeft() has reached zero? You haven't posted the code or the requirements for your timeLeft() function, so your readers can't tell what is supposed to be happening.

Resources