Derpy Mc Derpface
Derpy Mc Derpface

Reputation: 19

How to create a timer to reload a tableView on a fixed interval?

for a school project we need to create a application which refreshes its content on a fixed interval. The content is in a collectionView. The interval is once every 40 seconds. What would be the best approach to achieve this?

Upvotes: 1

Views: 583

Answers (3)

balkaran singh
balkaran singh

Reputation: 2786

Use the code below

NSTimer *current_timer = [NSTimer timerWithTimeInterval:10.0 target:self  selector:@selector(updateDefaultTimer:) userInfo:nil repeats:YES];

[[NSRunLoop mainRunLoop] addTimer:current_timer forMode:NSDefaultRunLoopMode];

- (void) updateDefaultTimer:(NSTimer *)timer{
   [self.yourCollectionView reloadData];
}

updateDefaultTimer will call every 10 sec

Upvotes: 1

mkz
mkz

Reputation: 2302

You can create repeated timer and reload your collection in its function.

var timer: NSTimer!
override func viewDidLoad() {
    super.viewDidLoad()
    timer = NSTimer.scheduledTimerWithTimeInterval(40.0, target: self, selector: #selector(reloadCollection), userInfo: nil, repeats: true)
}

func reloadCollection() {
    // your code here
    // ...
    collectionView.reloadData()
}

Upvotes: 0

Balaji Ramakrishnan
Balaji Ramakrishnan

Reputation: 1949

Try the following :

 NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:40 target:self selector:@selector(yourMethod) userInfo:nil repeats:YES];

yourMethod func

-(void)yourMethod{

  [self.yourCollectionView reloadData];
}

When you don't want to timer, stop the timer using invalidate.

[aTimer invalidate];

aTimer = nil;

Hope it helps..

Upvotes: 2

Related Questions