0

I've a strange problem. I have a code that works fine on iPhone Simulator of Xcode 3, but when I debug with an iPhone 3G it skips a part of code.

I have a UITableView that takes its data from a CSV on a server (it can read the file, which I've tested with a Log) but the table isn't populated. So I discover that it skips completely cellForRowAtIndexPath: method.

Here is the code:

    - (void)viewDidLoad {
    [super viewDidLoad];

    //Inizializzo la posizione di partenza e calcolo distanza
    CLLocation *startPos = [[CLLocation alloc]initWithLatitude:ST_LAT longitude:ST_LONG];
    CLLocationDistance distanza = [posizione distanceFromLocation:startPos];

    NSLog(@"Latitudine: %f",posizione.coordinate.latitude);
    NSLog(@"Longitudine: %f",posizione.coordinate.longitude);
    NSLog(@"Distanza (in metri): %f",distanza);

    //Creo una lista temporaneamente non ordinata
    NSMutableArray *listaNonOrdinata = [[NSMutableArray alloc]init];

    //Prelevo dati da CSV e li inserisco in record
//  NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Lista1" ofType:@"csv"] encoding:NSUTF8StringEncoding error:nil];
    NSString *fileString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URL_CSV] encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"fileString: %@", fileString);
    record = [fileString csvRows];

    //Inserisco titolo
    self.navigationItem.title = [[record objectAtIndex:0]objectAtIndex:C_TIPOLOGIA];

    //Creo oggetto doppio per verificare se è presente più di una volta
    id doppio = nil;

    //Controllo tutto il record e inserisco nella listaNonOrdinata solo i dati singoli
    //se il dato è doppio inserisco il rispettivo BOOL
    for (int i=1; i < record.count; i++) {
        //Carico un array temporaneo con tutti gli oggetti con chiave Item
        NSMutableArray *temp = [[NSMutableArray alloc]init];
        for (int j=0; j < listaNonOrdinata.count; j++) {
            [temp addObject:[[listaNonOrdinata objectAtIndex:j]objectForKey:@"Item"]];
        }
        //Controllo che nessuno di questi sia già presente
        doppio = [[record objectAtIndex:i] firstObjectCommonWithArray:temp];
        [temp release];

        //Inizializzo posizione oggetto
        NSNumberFormatter *form = [[NSNumberFormatter alloc]init];
        NSNumber *lat = [form numberFromString:[[record objectAtIndex:i]objectAtIndex:C_LAT]];
        NSNumber *longit = [form numberFromString:[[record objectAtIndex:i]objectAtIndex:C_LONG]];
        [form release];
        CLLocation *posObj = [[CLLocation alloc]initWithLatitude:(CLLocationDegrees)[lat floatValue] 
                                                       longitude:(CLLocationDegrees)[longit floatValue]];

        //Se mi trovo nel raggio
        if (distanza < RAGGIO) {
            //Controllo che l'oggetto si trovi entro il raggio
            if ([posObj distanceFromLocation:startPos] < RAGGIO) {
                //Se non è doppio lo inserisco con Bool NO
                if (doppio == nil) {
                    NSMutableDictionary *dizionario = [[NSMutableDictionary alloc]init];
                    [dizionario setObject:[[record objectAtIndex:i]objectAtIndex:C_TIPOLOGIA] forKey:@"Item"];
                    [dizionario setObject:[NSNumber numberWithBool:NO] forKey:@"Bool"];
                    [listaNonOrdinata addObject:dizionario];
                    [dizionario release];
                } else {
                    //Cerco l'elemento doppio e gli sostituisco il Bool a YES
                    for (int j=0; j < listaNonOrdinata.count; j++) {
                        if ([[[listaNonOrdinata objectAtIndex:j]objectForKey:@"Item"] isEqualToString:[[record objectAtIndex:i]objectAtIndex:C_TIPOLOGIA]]) {
                            NSMutableDictionary *dizionario = [[NSMutableDictionary alloc]init];
                            [dizionario setObject:[[record objectAtIndex:i]objectAtIndex:C_TIPOLOGIA] forKey:@"Item"];
                            [dizionario setObject:[NSNumber numberWithBool:YES] forKey:@"Bool"];
                            [listaNonOrdinata replaceObjectAtIndex:j withObject:dizionario];
                            [dizionario release];
                            break;
                        }
                    }
                }
            }
        } else {
            //Controllo che l'oggetto si trovi fuori dal raggio
            if ([posObj distanceFromLocation:startPos] > RAGGIO) {
                //Se non è doppio lo inserisco con Bool NO
                if (doppio == nil) {
                    NSMutableDictionary *dizionario = [[NSMutableDictionary alloc]init];
                    [dizionario setObject:[[record objectAtIndex:i]objectAtIndex:C_TIPOLOGIA] forKey:@"Item"];
                    [dizionario setObject:[NSNumber numberWithBool:NO] forKey:@"Bool"];
                    [listaNonOrdinata addObject:dizionario];
                    [dizionario release];
                } else {
                    //Cerco l'elemento doppio e gli sostituisco il Bool a YES
                    for (int j=0; j < listaNonOrdinata.count; j++) {
                        if ([[[listaNonOrdinata objectAtIndex:j]objectForKey:@"Item"] isEqualToString:[[record objectAtIndex:i]objectAtIndex:C_TIPOLOGIA]]) {
                            NSMutableDictionary *dizionario = [[NSMutableDictionary alloc]init];
                            [dizionario setObject:[[record objectAtIndex:i]objectAtIndex:C_TIPOLOGIA] forKey:@"Item"];
                            [dizionario setObject:[NSNumber numberWithBool:YES] forKey:@"Bool"];
                            [listaNonOrdinata replaceObjectAtIndex:j withObject:dizionario];
                            [dizionario release];
                            break;
                        }
                    }
                }
            }
        }
    }

    //Ordino listaNonOrdinata in ordine alfabetico
    lista = [[NSArray alloc]init];
    NSComparator comparatore = ^NSComparisonResult(id aDictionary, id anotherDictionary) {
        return [[aDictionary objectForKey:@"Item"] localizedCaseInsensitiveCompare:[anotherDictionary objectForKey:@"Item"]];
    };
    lista = [listaNonOrdinata sortedArrayUsingComparator:comparatore];
    [listaNonOrdinata release];
    [startPos release];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    //Retain:
    [lista retain];
    [record retain];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return lista.count;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...
    NSString *cellValue = [[lista objectAtIndex:indexPath.row]objectForKey:@"Item"];
    cell.textLabel.text = cellValue;

    NSLog(@"dettaglio bool Value: %@",[[[lista objectAtIndex:indexPath.row]objectForKey:@"Bool"]boolValue] ? @"YES" : @"NO");

    if ([[[lista objectAtIndex:indexPath.row]objectForKey:@"Bool"]boolValue]) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    [cellValue release];

    return cell;
}

The code is a bit complicated for other functions, but it should be necessary to understand the problem.

EDIT: I've solved the problem.. It didn't execute that piece of code because lista.count remained 0 and was not filled due to a wrong format string-to-number from the csv. I just changed a "." with a "," in each issue and then it worked ... I do not understand why the simulator worked with the device no, maybe the PC accepts commas and dots without problems

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Shafa95
  • 201
  • 2
  • 14

1 Answers1

0

If your cellForRowAtIndexPath: is not called, you have to check if datasource delegate is connected to the table (either in Interface builder, or assigned in code).

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I use `UITableViewController`, so I think that I can do all in this file without setting any delegate.. But I've tried putting this at the beginning of `ViewDidLoad`:`[self.tableView setDelegate:self]; [self.tableView setDataSource:self];` But the problem persists. – Shafa95 Aug 27 '13 at 12:46