Problem mit TableView

  • Problem mit TableView

    ich habe den nachfolgenden controller in meinem projekt. wenn ich ihn aufrufe und das feste array aus der viewdidload verwende, wird der tableview angezeigt.

    verwende ich aber die methode zum einlesen der daten stürzt die app ab. die daten sind aber im array drin, er zeigt mir bei numberOfRowsInSection die richtige anzahl der datensätze an.

    woran kann das liegen?

    Quellcode

    1. #import "FaelligVC.h"
    2. @interface FaelligVC ()
    3. @end
    4. @implementation FaelligVC
    5. - (id)initWithStyle:(UITableViewStyle)style
    6. {
    7. self = [super initWithStyle:style];
    8. if (self) {
    9. // Custom initialization
    10. }
    11. return self;
    12. }
    13. - (void)viewDidLoad
    14. {
    15. [super viewDidLoad];
    16. // listOfItems = [[NSMutableArray alloc] init];
    17. listOfItems = [[NSMutableArray alloc] initWithObjects:@"Januar", @"Februar", @"März", @"April", @"Mai", @"Juni", @"Juli", @"August", @"September", @"Oktober", @"November", @"Dezember", nil];
    18. // [self datenlesen1];
    19. }
    20. - (void)viewDidUnload
    21. {
    22. [super viewDidUnload];
    23. // Release any retained subviews of the main view.
    24. // e.g. self.myOutlet = nil;
    25. }
    26. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    27. {
    28. return (interfaceOrientation == UIInterfaceOrientationPortrait);
    29. }
    30. #pragma mark - Table view data source
    31. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    32. {
    33. // Return the number of sections.
    34. return 1;
    35. }
    36. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    37. {
    38. // Return the number of rows in the section.
    39. NSLog(@"%i",[listOfItems count]);
    40. return [listOfItems count];
    41. }
    42. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    43. {
    44. static NSString *CellIdentifier = @"Cell";
    45. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    46. if (cell == nil) {
    47. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    48. }
    49. // Configure the cell...
    50. cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
    51. return cell;
    52. }
    53. #pragma mark - Table view delegate
    54. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    55. {
    56. // Navigation logic may go here. Create and push another view controller.
    57. /*
    58. <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    59. // ...
    60. // Pass the selected object to the new view controller.
    61. [self.navigationController pushViewController:detailViewController animated:YES];
    62. [detailViewController release];
    63. */
    64. }
    65. -(void)datenlesen1 {
    66. NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"faellig" ofType:@"sqlite"];
    67. const char *dbpath = [databasePath UTF8String];
    68. sqlite3_stmt *statement;
    69. NSString *abfrageSQL = nil;
    70. if (sqlite3_open(dbpath, &faelligDB) == SQLITE_OK) {
    71. abfrageSQL = [NSString stringWithFormat:@"SELECT * FROM daten ORDER BY datum, was"];
    72. const char *abfrage_stmt = [abfrageSQL UTF8String];
    73. if (sqlite3_prepare_v2(faelligDB, abfrage_stmt, -1, &statement, NULL) == SQLITE_OK) {
    74. while (sqlite3_step(statement) == SQLITE_ROW) {
    75. // Datum
    76. char *db_datum = (char *)sqlite3_column_text(statement, 2);
    77. if (db_datum == NULL) db_datum = "";
    78. NSString *value_datum = [NSString stringWithUTF8String:db_datum];
    79. // Was
    80. char *db_was = (char *)sqlite3_column_text(statement, 3);
    81. if (db_was == NULL) db_was = "";
    82. NSString *value_was = [NSString stringWithUTF8String:db_was];
    83. NSString *item;
    84. item = [NSString stringWithFormat:@" %@ %@",value_datum, value_was];
    85. [listOfItems addObject:value_datum];
    86. // NSLog(@"%i %@", [listOfItems count], item);
    87. } // ENDE WHILE
    88. } // ENDE IF
    89. } // ENDE IF
    90. sqlite3_close(faelligDB);
    91. [databasePath release];
    92. } // ENDE DATENLESEN
    93. @end
    Alles anzeigen
  • Verwendest Du in dem Projekt ARC oder manuelle Speicherverwaltung?

    Bei ARC solltest Du auf alle Fälle die Setter für die Instanz-Variablen verwenden, damit ARC erkennt, dass die Referenz noch benötigt wird. Also immer self.listOfItems!

    Das Property für listOfItems solltest Du dann mit dem Attribut strong definieren, wobei strong der Default ist, wenn Du nicht explizit weak verwendest. ;)
  • Also die Tatsache, dass Xcode 4, mit den Default Einstellungen, bei einem Crash in die main Funktion springt kenne ich unter Xcode 3 nicht. Mir ist auch nicht klar, welche Information Apple dem Entwickler damit geben möchte. Diese Info ist nicht nur nutzlos, sondern auch noch total verwirrend.
  • habe mir mal das crash log vom ipad geholt

    wenn ich breakpoints setze, geht er mal bis zur numberOfSectionsInTableView, im anderen versuch bis zur numberOfRowsInSection oder sogar bis zur cellForRowAtIndexPath. ich habe in allen methoden

    Quellcode

    1. NSLog(@"%i",[listOfItems count]);


    drin und er zeigt die richtige anzahl an.

    die einstellung, dass er zum fehler springt, habe ich nicht gefunden. kann jemand mir sagen, wo ich das einsellen kann.
  • Ah ok, das Problem ist die folgende Zeile:

    Quellcode

    1. [databasePath release];

    , da

    Quellcode

    1. NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"faellig" ofType:@"sqlite"]

    einen Autorelease NSString zurück liefert!

    Um diese Fehler schnell zu finden, solltest Du die App hin und wieder mal mit "Build and Analyze" bauen. Für die Zeile

    Quellcode

    1. [databasePath release];

    solltest Du dann einen entsprechenden Hinweis erhalten.