iosuitableviewlocation-based

UITableView only updating the last cell. It displays all the information for the other 15, but only updates the 16th cell on NSTimer call


I am building an application that Displays the distance and heading to a building from the Users Location. Everything works fine and it updates perfectly for the last cell. Displaying changes in both the distance and the heading image. However, The first 15 cells after the initial cellForRowAtIndexPath creates them does not update. The update is called on a NSTimer every 1 second that reloads the tableView. I have tried using tags and running it without tags. Below is my code. I appreciate any help.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    static int locationTags[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        CNULocation * curLocation = [self.locationArray objectAtIndex:[indexPath row]];

        //NSLog(@"@@@@@ LOCATION IS : %@", curLocation.name);

        coord.latitude = curLocation.location.latitude;
        coord.longitude = curLocation.location.longitude;

        cell.textLabel.text = [curLocation name];
        cell.imageView.image = [curLocation icon];

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            dist = [[UILabel alloc] initWithFrame:CGRectMake(180,10,180,24)];
            imgView = [[UIImageView alloc] initWithFrame:CGRectMake(290,10,24,24)];

        } else {
            dist = [[UILabel alloc] initWithFrame:CGRectMake(440,10,180,24)];
            imgView = [[UIImageView alloc] initWithFrame:CGRectMake(640,10,24,24)];
        }
        [imgView setImage:[UIImage imageNamed:@"193-location-arrow copy.png"]];


        atloc = false;
        GeoAngle = [self setLatLonForDistanceAndAngle:userLoc];
        imgView.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);


        dist.opaque = false;
        if(boolyard && !atloc){
            dist.text = [[NSString alloc] initWithFormat:@"%.02f yards",distance];
            //NSLog(@"@@@@@@@Distance in yards@@@@@@@: %f", distance);
        }else if(!boolyard && !atloc) {
            dist.text = [[NSString alloc] initWithFormat:@"%.02f feet",distance];
            //NSLog(@"@@@@@@@Distance in feet@@@@@@@: %f", distance);
        }else if(atloc){
            dist.text = @"You are here!";
            [imgView setImage:nil];
        }

        dist.backgroundColor = [UIColor clearColor];
        dist.textAlignment   = UITextAlignmentRight;

        if (tags > 15) {
            tags = 0;
        }
        int ViewTags = locationTags[tags];
        cell.tag = ViewTags;
        tags++;
        //NSLog(@"Tags: %i",tags);
        dist.tag= ViewTags;
        imgView.tag = ViewTags;

        [cell.contentView addSubview:imgView];
        [cell.contentView addSubview:dist];

    }else {

        if (tags > 15) {
            tags = 0;
        }

        int ViewTags = locationTags[tags];
        cell.tag = ViewTags;
        tags++;
        imgView.tag = ViewTags;
        dist.tag=ViewTags;

        dist.opaque = true;
        imgView.opaque = true;

        //[imgView setImage:[UIImage imageNamed:@"193-location-arrow copy.png"]];

        GeoAngle = [self setLatLonForDistanceAndAngle:userLoc];
        imgView.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);



        if(boolyard && !atloc){
            dist.text = [[NSString alloc] initWithFormat:@"%.02f yards",distance];
            //NSLog(@"@@@@@@@Distance in yards@@@@@@@: %f", distance);
        }else if(!boolyard && !atloc) {
            dist.text = [[NSString alloc] initWithFormat:@"%.02f feet",distance];
            //NSLog(@"@@@@@@@Distance in feet@@@@@@@: %f", distance);
        }else if(atloc){
            dist.text = @"You are here!";
            [imgView setImage:nil];
        }

    }   

    //boolyard = true;
    //atloc = false;
    return cell;
}

Solution

  • I believe that "else" should not be here.

    if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    ...
        } else {
    ...
    }