How can i highlight a uitableviewcell only for a specific period of time without selecting it? I need to keep a cell highlighted for suppose 3 seconds and it then get dehighlighted. Basically i am developing a book type application where cells are in synch with audio. i have populated the tableviewcells with text and also I have timings for how long to keep a cell highlighted.
The API for setting a UITableViewCell's highlight state is setHighlighted:animated: so assuming you just need a simple highlight the work is half done.
Using the UITableViewDelegate method tableView:willDisplayCell:forRowAtIndexPath:, notify the cell that it has become visible through some method you define in the subclass. That method will cause the cell to highlight itself and start a timer for as long as you want the cell to remain highlighted. When the timer fires, cause the cell to remove the highlight.
You will of course have to guard against timers remaining active if a cell scrolls out of view, so invalidate timers and reset the highlight state during prepareForReuse:
I think you should make custom UITableViewCell class. When cell will be initialized, use a NSTimer with a specific time interval. Make a custom method and pass it in below NSTimer method as selector (in custom cell class)--
[NSTimer timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats]
In selector method you can set the tableViewCell background colour to show it highlighted.
Related
I'm showing Countdown timer in my Tableview cells for every product, having limited time offers.
As i scroll my tableview the values of timer are reflected to other cells. I know it is due to reuse of table view cells.
I want to show countdown timer for each product which are continuously changing for each product.
Any help will be appreciated.
Thanks in advance
You need to add observer in awakeFromNib() of your custom tableview cell class with a target function which update your UI of the cell. Then you need to declare a NSTimer variable inside your ViewController with target function which post the notification in regular intervals .You can set the initial values in cellForRowAtIndexPath method of the tableview.
I am able to detect when a cell is removed from the UITableView by writing a handler for the method tableView:didEndDisplayingCell:forRowAtIndexPath:. This method is called whenever a cell is removed from the display. However, there is one exception.
When the cell has a UITextField and the field is the first responder, this method is never called even when it's scrolled off the display and the cells immediately before it and after it are.
The cell is also confirmed to be removed from the UITableView with a test while the cell is scrolled off the screen. The call to cellForRowAtIndexPath: returns nil under this condition.
I also subclassed the UITableViewCell and wrote a handler for removeFromSuperView. Again this method is called for all the cells when the are scrolled off the screen except when the cell has a UITextField and it is the first responder.
Another thing to note is that the UITextField in the cell accepts key input while it is scrolled off the screen and the call to cellForRowAtIndexPath: returns nil. I can see this when the cell is scrolled back into view.
Does anyone have any solutions for detecting when the cell is scrolled out of view, so that the controller can get access to the UITextField?
You could try to resign your first responder manually before the cell disappears. Depending on your requirements, this could be done in multiple ways, usually when the user starts scrolling. You could restore the first responder after he finishes scrolling, if the cell is still visible. Probably better from the graphical design point of view as well.
Alternatively, you could try to implement delegate's tableView:willDisplayCell:forRowAtIndexPath: and make a previous visible cells set, which you'd intersect with tableview's visibleCells array. The elements that are not in the current visibleCells but are in the previous were removed. Then assign a copy of visibleCells to previousVisibleCells.
This is probably a bug in Apple's code, you could file a radar for it.
I have a timer on multiple cell which updates every second. how can i implement NSTimer scheduledTimerWithTimeInterval in each cell. Is it possible to use single Timer which keeps track of the time of each cell. How can i implement this?.
Since the table view cells are made up of UI elements, I don't believe you can have dynamic timers as such. However, you could reload the table view every second, and you could set the timer to the appropriate value in each cell when reconfiguring the cells (which will happen after a call to [tableView reloadData]).
I am currently developing an app for iOS 5 and above in which a video is played inside a custom UITableViewCell using an instance of AVQueuePlayer.
There are 'n' number of such custom cells playing 'n' number of videos.
I want to implement a functionality which disables the player from playing the video after a given time.
I have a countdown timer which displays the time left in a UILabel for disabling the player beneath the instance of AVQueuePlayer.
I have to update the timer after a minute(suppose) to show the time left for the disabling to take place. e.g. "5 mins left".
I am using NSTimer for this purpose. But, I dont know how to only reload or update the UILabel instance of the custom UITableViewCell. I have seen in some threads the use of the following method
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
But, if I do so when the video is playing, it stops and gets reloaded again.
Is there a better solution to this issue?
You could either save a reference to your cell when you create it (sounds like you're only making one of each cell type, even though you have a tableview); or grab the cell from the table and pull the label out of it.
I'd be tempted to set the tag of the label, then use that to get the label back.
When you create the label (or set it in interface-builder) call [myLabel setTag:LABEL_TAG];
Then later you can:
UITableViewCell * myCell = self.myTableView cellForRowAtIndexPath:LABLE_CELL_POSITION];
UILabel * myLabel = [cell viewWithTag:LABEL_TAG];
myLabel.setText:#"my new value";
In the above LABLE_CELL_POSITION would be position of the cell in your tableview (0 to ...); and LABEL_TAG is any number you want to use to denote that view, maybe 1234.
I am a bit confused and don't have much else to turn to. I have an iOS application that gets messages from a server and populates it into a cell in a UITableView. Every time a certain object gets updated from the server that update triggers a cell animation. In our case we have a table of scores in a game. If someone loses a point their cell would flash red and if they gain a point their cell would flash green. Simple concept that works well and our users like the feedback. What they don't like is how when they scroll they see the cells have random colors associated with it and stay there for about a second. I believe that it has something to do with the way that the cells are being recycled. I tried various checks regarding this issue, checking that the cell has the same value, setting the color manually to white, etc. and non of these methods work.
All in all I am trying to only show the animation when the cell has been in view and not when the users is scrolling through the UITableView. Anyone have any advice or ideas?
Thanks
What is happening
Since table views reuse their cells you are ending up reused cells that is being flashed even though they shouldn't.
How to solve it
You can restore the reused cell to its default state either in preprareForReuse: on the custom table view cell or in the tableView:willDisplayCell:forRowAtIndexPath: delegate method (as ACB mentioned).
Since you are animating with UIView animations, you only need to change the value of the animated property to cancel the animation and return to the default state.
If you would have user Core Animation for the flash
If the animation was done using Core Animation you would instead have called removeAllAnimations on the animating layer. (For a flashing animation you probably wouldn't change the property on the layer, instead you would tell the animation to autoreverse. This would mean that removing the animation would change the color back to its original value.)
One option is to remove the animation and reset the color in willDisplayCell delegate method to the default color. If that doesn't work you can set the dequeueReusableCellWithIdentifier as follows,
NSString *aCellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:aCellIdentifier];
But this will be a problem if you have a lot data to display as it will create memory issues. Note that in this case you will not be reusing the table cells.