Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSTimer, UITableView and access NSMutableArrays

tomdekuijpertomdekuijper Posts: 4
edited January 2010 in iOS SDK Development
Hello,

I am trying to make a stopwatch function which would seem to me very simple. However I am struggeling on a number of topics.
I have set up a timer (scheduledTimerWithTimeInterval) and the stopwatch is running fine. However I cannot access the NSMutableArray
i have created in the selector method for the timer. I am using a NSMutableArray to record the different NSDates every time the users
presses the ' LAP' button. In the TableView i am update the rows with these laptimers but get bad access error on the following code:
-(UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString * const kLapCellID = @LapsCellID;
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:kLapCellID];
if (cell ==nil) {
cell = [[[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:kLapCellID] autorelease];
}
//calculate lap
NSInteger index = indexPath.row;
NSDate *endOfLap = [self.Laps objectAtIndex:index+1];
NSDate *startOfLap = [self.Laps objectAtIndex:index];
NSTimeInterval lapTime = [endOfLap timeIntervalSinceDate:startOfLap];
    int minutes = fmod((lapTime / 60), 60);
int seconds = fmod(lapTime, 60);
int hunderths = (fmod(lapTime, 60)-seconds) * 100;
cell.textLabel.text = [NSString stringWithFormat:@Lap %d  %02d:%02d:%02d,
(indexPath.row+1), minutes, seconds, hunderths];
return cell;
}

How can I overcome this? And how can I ensure NSMutableArrays can be used throughout the code???

many thx in advance,

tom
Post edited by tomdekuijper on

Replies

  • SkyTrixSkyTrix Posts: 106
    edited January 2010
    Could you run the debugger? On what line is it going wrong?
    My guess is here:
    cell.textLabel.text = [NSString stringWithFormat:@Lap %d  %02d:%02d:%02d,
    (indexPath.row+1), minutes, seconds, hunderths];
    
    Why? I'm simply not familiar with your %02d placeholder. The placeholder for an integer is %i. I don't think the one you use exists, but I could be wrong..

    Edit:
    My bad on your placeholders. It has the same use as in C's printf.
    Available for hire: <a href="http://www.freshcreations.be"; target="_blank">FreshCreations</a>
  • tomdekuijpertomdekuijper Posts: 4
    edited January 2010
    It is going wrong on this line:
    NSTimeInterval lapTime = [endOfLap timeIntervalSinceDate:startOfLap];
    

    Main reason is because the fetching of the startofLap and endOfLap returns nil. Even more bizarre is that the first lap is displayed correctly. however as soon as i run the second row (or lap) the application crashes. I have set the NumberOfRowsInSection at Laps.count-1 so I am positive that the index to access the array is not out of bounds.
  • SkyTrixSkyTrix Posts: 106
    edited January 2010
    You're indeed in big trouble of those return nil. Could you show your code on how you add those laps to the NSMutableArray?
    Available for hire: <a href="http://www.freshcreations.be"; target="_blank">FreshCreations</a>
  • tomdekuijpertomdekuijper Posts: 4
    edited January 2010
    init of the array
    self.Laps = [[NSMutableArray alloc] initWithCapacity:1];

    adding the laps:

    <code>if (isRunning) {
    NSDate *currenttime = [NSDate date];
    [self.Laps addObject:currenttime];
    self.lastLap = [self.Laps objectAtIndex:(self.Laps.count-1)];
    [currenttime release];
    [self.tableview reloadData];
    }
    </code>
  • SkyTrixSkyTrix Posts: 106
    edited January 2010
    Looks good, although you can change
    self.lastLap = ..
    
    to
    self.lastLap = [self.Laps lastObject];
    
    (just a tip)

    Are you sure they are added to the array? (because you're dealing with a BOOL) Added correctly and only going wrong in your table?
    init of the array
    self.Laps = [[NSMutableArray alloc] initWithCapacity:1];

    adding the laps:

    <code>if (isRunning) {
    NSDate *currenttime = [NSDate date];
    [self.Laps addObject:currenttime];
    self.lastLap = [self.Laps objectAtIndex:(self.Laps.count-1)];
    [currenttime release];
    [self.tableview reloadData];
    }
    </code>
    Available for hire: <a href="http://www.freshcreations.be"; target="_blank">FreshCreations</a>
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    edited January 2010
    self.Laps =  [[NSMutableArray alloc] initWithCapacity:1];
    

    This is a leak. See the properties link in my signature for more info.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • tomdekuijpertomdekuijper Posts: 4
    edited January 2010
    Sorry I am still struggeling:

    1. in the class definition is define the first instance variable (in the header file)
    NSMutableArray *Laps
    2. i declare the property as (nonatomic, retain) NSMutableArray *Laps (in the header file)
    3. i synthesize the property in my .m file as synthesize Laps
    4. in viewDidLoad I init the array through the following code:
    self.Laps = [[NSMutableArray alloc] init];
    5. when adding objects I use the following
    [self.Laps addObject:aObject];
    accessing object with the following code
    NSDate *aDate = [self.Laps objectAtIndex:index];
    6.in viewDidUnLoad I nil the array through the property
    self.Laps = nil;
    7.in dealloc i release it with [self.Laps release]; the variable itself i believe


    I might be ignorant but what I am missing? Although I believe you're correct because it getting undesirable results when accessing items in the array. Is the NSTimer of any influence?
Sign In or Register to comment.