TableView json Problem

  • TableView json Problem

    Hallo,

    ich habe ein Problem beim Laden einer json Datei in eine TableView.
    Bis jetzt hat dieser Code funktioniert. Seit ich aber xcode 5 benutzte stürzt mir der IOS Simulator ab, soblad er die Daten in die Tabellenzelle schreiben soll.
    Ich hoffe mi kann da jemand weiterhelfen.

    news ist ein NSArray

    Quellcode

    1. - (int)numberOfSelectionInTableView:(UITableView *)tableView
    2. {
    3. return 1;
    4. }
    5. - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    6. {
    7. return [news count];
    8. }
    9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    10. {
    11. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    12. if(cell == nil)
    13. {
    14. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    15. }
    16. cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"head"]; <-hier ist dann Schluss
    17. cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    18. cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"date"];
    19. cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
    20. return cell;
    21. }
    22. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    23. {
    24. DetailNews2Controller *detailNewsController = [[DetailNews2Controller alloc] initWithNibName:@"DetailNews2Controller" bundle:nil];
    25. detailNewsController.title = [[news objectAtIndex:indexPath.row] objectForKey:@"head"];
    26. detailNewsController.newsArticle = [news objectAtIndex:indexPath.row];
    27. [self.navigationController pushViewController:detailNewsController animated:YES];
    28. }
    Alles anzeigen
  • In numbersofrows solltest du auf jedem Fall abprüfen ob News da ist und ob's nen count größer 0 gibt

    Sonst crashed es schon da

    Und da wo Schluss ist solltest du auch prüfen

    news && news count größer indexpath.row

    Aber da wirft Xcode mit 100% Sicherheit ne Fehlermeldung , was sagt die denn?
    Ich weiß nicht immer wovon ich rede aber ich weiß das ich Recht habe. :saint:
  • news wird so befüllt.

    Quellcode

    1. - (void)viewDidLoad
    2. {
    3. [super viewDidLoad];
    4. self.title = @"Aktuelles";
    5. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    6. NSURL *url = [NSURL URLWithString:@"http://domain/news_json.php"];
    7. NSURLRequest *request = [NSURLRequest requestWithURL:url];
    8. (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
    9. }
    10. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    11. {
    12. data1 = [[NSMutableData alloc] init];
    13. }
    14. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
    15. {
    16. [data1 appendData:theData];
    17. }
    18. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    19. {
    20. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    21. news = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:nil];
    22. [mainTableView reloadData];
    23. }
    24. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    25. {
    26. UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"Es konnte keine Verbindung zum Server aufgebeut werden. Bitte überprüfen Sie ihre Online Verbindung" delegate:nil cancelButtonTitle:@"Schließen" otherButtonTitles:nil];
    27. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    28. [errorView show];
    29. }
    30. - (int)numberOfSelectionInTableView:(UITableView *)tableView
    31. {
    32. return 1;
    33. }
    34. - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    35. {
    36. return [news count];
    37. }
    Alles anzeigen


    was ich nicht verstehe den selben code nutze ich in einer anderen App ohne Probleme. Habe sogar zum testen die URL übernommen.
    Daten werden auch vom Server einwandfrei bereit gestellt.
  • Du solltest versuchen beim Zugriff auf Properties, zumindest bei Wertzuweisungen, die Setter zu verwenden, also anstelle von:

    Quellcode

    1. news = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:nil];

    Quellcode

    1. self.news = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:nil];

    Dann funktioniert auch das eingestellte Ownership vom Property und das zugewiesene Objekt, also das Ergebnis von JSONObjectWithData:options:error:, wird nicht durch den Autorelease Pool freigegeben. ;)