UITableView crash: "unrecognized selector sent to instance"

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • UITableView crash: "unrecognized selector sent to instance"

    Mein UITableView fliegt mir gerade um die Ohren und ich verstehe nicht ganz, warum.

    Ich unterscheide im Controller zwischen Section 1 und 2 für das Custom-Layout.
    Der Fehler kommt nicht immer und wenn, dann nur beim scrollen und dann heißt es entweder:

    Quellcode

    1. "[UILabel setProgress:]: unrecognized selector sent to instance"

    oder auch manchmal:

    Quellcode

    1. "[UIProgressView setText:]: unrecognized selector sent to instance"


    Ich weiss nicht ansatzweise, wo das Problem liegt,
    deshalb schmeiß ich hier auch gleich etwas brutal viel Code in den Thread.

    Mache ich evtl. grundlegend etwas falsch bei meinen Customized-TableViewCells?

    Hier mein cellForRowAtIndexPath:

    Quellcode

    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    2. static NSString *CellIdentifier = @"Cell";
    3. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    4. if (cell == nil) {
    5. if (indexPath.section == 0)
    6. cell = [self getCellContentViewStart:CellIdentifier];
    7. else
    8. cell = [self getCellContentViewContinue:CellIdentifier];
    9. }
    10. // Configure the cell...
    11. if (indexPath.section == 0) {
    12. UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
    13. UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
    14. lblTemp1.text = @"neues Rätsel starten";
    15. lblTemp2.text = [NSString stringWithFormat:@"noch %i Rätsel nicht begonnen",[fileListArray count]-rowCount];
    16. }
    17. else {
    18. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    19. NSString *loadFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[myAnswers objectAtIndex:indexPath.row]];
    20. NSMutableArray *loadArray = [NSMutableArray arrayWithContentsOfFile:loadFilePath];
    21. UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
    22. UIProgressView *progressTemp = (UIProgressView *)[cell viewWithTag:2];
    23. UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:3];
    24. UIButton *buttonTemp = (UIButton *)[cell viewWithTag:4];
    25. [buttonTemp addTarget:self action:@selector(delAlert:) forControlEvents:UIControlEventTouchUpInside];
    26. buttonTemp.tag = indexPath.row;
    27. lblTemp1.text = [NSString stringWithFormat:@"%@ Nr.%i",packName,indexPath.row+1];
    28. progressTemp.progress = [[loadArray objectAtIndex:2] floatValue];
    29. int percent = ([[loadArray objectAtIndex:2] floatValue] / [[loadArray objectAtIndex:1] floatValue])*100;
    30. lblTemp2.text = [NSString stringWithFormat:@"Gelöst: %i\%% Spielzeit: %@ min",percent,[loadArray objectAtIndex:3]];
    31. }
    32. return cell;
    33. }
    Alles anzeigen


    Und hier die zugehörige Methoden für meine Custom-Cell:

    Quellcode

    1. - (UITableViewCell *) getCellContentViewStart:(NSString *)CellIdentifier {
    2. CGRect CellFrame = CGRectMake(0, 0, 300, 70);
    3. CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
    4. CGRect Label2Frame = CGRectMake(10, 33, 290, 25);
    5. UILabel *lblTemp;
    6. UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
    7. //Initialize Label with tag 1
    8. lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    9. lblTemp.tag = 1;
    10. [cell.contentView addSubview:lblTemp];
    11. [lblTemp release];
    12. //Initialize Label with tag 2
    13. lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
    14. lblTemp.tag = 2;
    15. lblTemp.font = [UIFont boldSystemFontOfSize:12];
    16. lblTemp.textColor = [UIColor lightGrayColor];
    17. [cell.contentView addSubview:lblTemp];
    18. [lblTemp release];
    19. return cell;
    20. }
    21. - (UITableViewCell *) getCellContentViewContinue:(NSString *)CellIdentifier {
    22. CGRect CellFrame = CGRectMake(0, 0, 300, 80);
    23. CGRect Label1Frame = CGRectMake(55, 5, 290, 25);
    24. CGRect ProgressFrame = CGRectMake(55, 38, 210, 25);
    25. CGRect Label2Frame = CGRectMake(55, 44, 290, 25);
    26. CGRect ButtonFrame = CGRectMake(5, 5, 45, 70);
    27. UIButton *buttonTemp = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    28. UILabel *lblTemp;
    29. UIProgressView *progressTemp;
    30. UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
    31. //Initialize Label with tag 1
    32. lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    33. lblTemp.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    34. lblTemp.tag = 1;
    35. lblTemp.font = [UIFont systemFontOfSize:14];
    36. [cell.contentView addSubview:lblTemp];
    37. [lblTemp release];
    38. //Initialize ProgrssBar with tag 2
    39. progressTemp = [[UIProgressView alloc] initWithFrame:ProgressFrame];
    40. progressTemp.tag = 2;
    41. [cell.contentView addSubview:progressTemp];
    42. [progressTemp release];
    43. //Initialize Label with tag 3
    44. lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
    45. lblTemp.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    46. lblTemp.tag = 3;
    47. lblTemp.font = [UIFont boldSystemFontOfSize:12];
    48. lblTemp.textColor = [UIColor lightGrayColor];
    49. [cell.contentView addSubview:lblTemp];
    50. [lblTemp release];
    51. //Initialize Button with tag 4
    52. buttonTemp.tag = 4;
    53. [buttonTemp setTitle:@"reset" forState:UIControlStateNormal];
    54. buttonTemp.frame = ButtonFrame;
    55. buttonTemp.backgroundColor = [UIColor clearColor];
    56. [buttonTemp setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    57. [buttonTemp setFont:[UIFont boldSystemFontOfSize:12]];
    58. [cell.contentView addSubview:buttonTemp];
    59. [buttonTemp release];
    60. return cell;
    61. }
    Alles anzeigen


    Viele Grüße
    dealloc {
    [myLeak retain];
    }
  • fwtag schrieb:

    Wie lautet denn die komplette Konsolen-Ausgabe (ich meine die vollständigen Zeilen)?


    Quellcode

    1. 2010-10-18 15:34:54.507 Krupion Kreuzwort[4560:207] -[UIProgressView setText:]: unrecognized selector sent to instance 0x6437020
    2. 2010-10-18 15:34:54.509 Krupion Kreuzwort[4560:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIProgressView setText:]: unrecognized selector sent to instance 0x6437020'
    3. *** Call stack at first throw:
    4. (
    5. 0 CoreFoundation 0x028adb99 __exceptionPreprocess + 185
    6. 1 libobjc.A.dylib 0x029fd40e objc_exception_throw + 47
    7. 2 CoreFoundation 0x028af6ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    8. 3 CoreFoundation 0x0281f2b6 ___forwarding___ + 966
    9. 4 CoreFoundation 0x0281ee72 _CF_forwarding_prep_0 + 50
    10. 5 Krupion Kreuzwort 0x0001e1cd -[DetailPackage tableView:cellForRowAtIndexPath:] + 433
    11. 6 UIKit 0x0037bd6f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
    12. 7 UIKit 0x00371e02 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    13. 8 UIKit 0x0038669f -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1348
    14. 9 UIKit 0x0037e7ec -[UITableView layoutSubviews] + 242
    15. 10 QuartzCore 0x025be481 -[CALayer layoutSublayers] + 177
    16. 11 QuartzCore 0x025be1b1 CALayerLayoutIfNeeded + 220
    17. 12 QuartzCore 0x025b72e0 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
    18. 13 QuartzCore 0x025b7040 _ZN2CA11Transaction6commitEv + 292
    19. 14 QuartzCore 0x025e7ebb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    20. 15 CoreFoundation 0x0288ef4b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    21. 16 CoreFoundation 0x02823b27 __CFRunLoopDoObservers + 295
    22. 17 CoreFoundation 0x027ecce7 __CFRunLoopRun + 1575
    23. 18 CoreFoundation 0x027ec350 CFRunLoopRunSpecific + 208
    24. 19 CoreFoundation 0x027ec271 CFRunLoopRunInMode + 97
    25. 20 GraphicsServices 0x0318c00c GSEventRunModal + 217
    26. 21 GraphicsServices 0x0318c0d1 GSEventRun + 115
    27. 22 UIKit 0x00318af2 UIApplicationMain + 1160
    28. 23 Krupion Kreuzwort 0x00001d80 main + 102
    29. 24 Krupion Kreuzwort 0x00001d11 start + 53
    30. )
    31. terminate called after throwing an instance of 'NSException'
    Alles anzeigen
    dealloc {
    [myLeak retain];
    }
  • (Das ist immer das Blöde, wenn man nicht an seinem Entwicklungsrechner sitzt).

    Du kannst in den Debugger schauen: Links siehst den Stack, davon sind eine ganze Menge zeilen grau und mindestens eine schwarz. Die oberste der schwarzen Zeilen anklicken und Du landest direkt beim Aufruf, der den Fehler verursacht hat (in UTIableView:cellForRowAtAtIndexPath: ). Dort verwendest Du setProgress: oder setText:, obwohl diese Objekte diesen Methodenaufrauf nicht kennen.
  • Ok, ich habe was gefunden und jetzt verstehe ich garnichts mehr.

    Was hab ich gemacht?
    Ich habe die tags geändert, so dass ein Label, egal zu welcher section es gehört,
    immer das gleiche Tag hat.

    Nun stürzt es nicht mehr ab aber,
    scrolle ich im TableView ein paar mal hoch unt runter, dann vertauschen sich die Inhalte der cells.

    Ein Beispiel:
    In indexRow.section == 0 sind zwei Labels
    In indexRow.section == 1 befinden sich immer 2 Labels, eine ProgressBar und ein Button.
    ich Scrolle nach ganz unten und wieder ganz nach oben, Ergebniss:
    Der Inhalt von section 0 wird zum inhalt von einem willkürlichen row aus section 1.

    ...soweit ich weiss, zeichnet der TableView nur die Inhalte, die er gerade Anzeigen muss.
    Anscheinend wirft er in meinem Fall, beim scrollen was durcheinander.

    Hab beschriebens Beispiel mal als ScreenShot angehängt.
    dealloc {
    [myLeak retain];
    }
  • Das liegt mit ziemlicher Sicherheit an der Wiedervendung der Zellen (reuseIdentifier). Du müsstest dann für die unterschiedlichen Sections unterschiedliche Identifiers verwenden (ich mache das z. B. mit einem NSString, der u.a. die Section als Zahlenwert enthält).

    Zurück zu Deiner Exception: Funktioniert es jetzt, weil Du die Tag-Sache gemacht hast? Dann habe ich so meine Zweifel ...
  • Das war's, vielen dank! :)

    Quellcode

    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    2. static NSString *CellIdentifier;
    3. if (indexPath.section == 0) CellIdentifier = @"CellForSectionZero";
    4. else CellIdentifier = @"CellForSectionOne";
    5. ....
    dealloc {
    [myLeak retain];
    }