Re: NSInvocation or NSTimer with an arg
Re: NSInvocation or NSTimer with an arg
- Subject: Re: NSInvocation or NSTimer with an arg
- From: "Hank Heijink (Mailinglists)" <email@hidden>
- Date: Wed, 16 Sep 2009 21:08:27 -0400
On Sep 16, 2009, at 8:24 PM, Luther Baker wrote:
I'd like to fire a simple callback every .1 of second (yes, my
stopwatch
style app). But in that callback, I need access to a corresponding
UITableViewCell. Unfortunately, I don't see a way to choose a
selector with
a UITableViewCell argument for the oneliner:
[NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)ti#>
target:<#(id)aTarget#> selector:<#(SEL)aSelector#> userInfo:<#(id)
userInfo#>
repeats:<#(BOOL)yesOrNo#>
Yep, that's what the userInfo parameter is for. It's an id, so you can
pass your UITableViewCell there, and get it back in the method you
pass in the selector argument by calling the -userInfo method on the
NSTimer instance (see below).
[...snip approach using NSInvocation...]
but this approach requires roughly 7 extra lines of code every time I
configure a cell for display (creating things like NSMethodSignature,
NSInvocation, setters, etc). It isn't enormous but suddenly, my
generally
small cellForRowAtIndexPath is getting pretty long. I'm wondering if I
should be careful. Things like scrolling etc might blast through
this and
I'd prefer to keep the method fairly light if, there was in fact a
way to
use the simpler one liner to invoke a callback with an arg.
Passing UITableViewCells around is risky business: if the cell scrolls
off the screen, you're pointing to a cell in the reuse pool (provided
you stick to the standard paradigm), which will most likely lead to
trouble, depending on what you're doing. You might be better off
passing an NSIndexPath, and then asking for -[UITableView
cellForRowAtIndexPath:], which will return nil if the cell is not
visible.
Again. I certainly appreciate any informal feedback/suggestions. Maybe
userInfo would apply for what I'm doing here? I thought userInfo
would be
stored in the NSTimer and unfortunately, the single callback that
everything
is firing to doesn't take the NSTimer as an arg so I think that
means I'd be
tracking NSTimers external to the callback loop ... and I'd just
rather
avoid that.
Are you sure your method has the right signature? If you pass a
selector that doesn't accept arguments, everything will work, but see
the following from the documentation of NSTimer:
The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
Hope this helps,
Hank
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden