0

我正在构建一个应用程序,它显示从用户位置到建筑物的距离和方向。一切正常,并且为最后一个单元格完美更新。显示距离和航向图像的变化。但是,初始 cellForRowAtIndexPath 创建后的前 15 个单元格不会更新。每 1 秒在 NSTimer 上调用一次更新,以重新加载 tableView。我尝试过使用标签并在没有标签的情况下运行它。下面是我的代码。我很感激任何帮助。

    - (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;
}
4

1 回答 1

1

我认为“其他”不应该在这里。

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

...
    } else {
...
}
于 2012-08-08T09:29:47.717 回答