Hinzufügen von Buttons bringt custom TableViewCell durcheinander (falsche indexpath.row bei Action)

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

  • Hinzufügen von Buttons bringt custom TableViewCell durcheinander (falsche indexpath.row bei Action)

    Hallo,
    ich habe mal wieder ein kleines Problem und würde mich über Hilfe freuen :)

    Für mein TableView nutze ich eine Postum TableViewCell (erstellt im IB). Je nach Section fülle ich sie unterschiedlich. Das funktioniert auch soweit ganz gut. Die Cell hat verschiedene Labels und immer einen detailbutton. je nach Sektion dann entweder einen löschen Button oder Play/Pause und löschen.

    Quellcode

    1. // give each row a content
    2. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    3. {
    4. static NSString *MyIdentifier = @"CellForRecord";
    5. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    6. //...
    7. //set the event for each state
    8. BigEvent* event;
    9. //fill event
    10. //configure labels for available information
    11. if( (indexPath.section == 1 && [self.model countOfSchedulingList]>0) ||
    12. (indexPath.section == 2 && [self.model countOfRecordedList]>0) )
    13. {
    14. if (cell == nil)
    15. {
    16. UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"RecordCell" bundle:nil];
    17. cell = (RecordCellVC *)temporaryController.view;
    18. [temporaryController release];
    19. cell.selectionStyle = UITableViewCellSelectionStyleGray;
    20. }
    21. //....
    22. //detailbutton
    23. UIButton *detailbutton = (UIButton*) [cell viewWithTag:4];
    24. [detailbutton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchDown];
    25. NSString* path = [[NSBundle mainBundle] pathForResource:@"button-more_red" ofType:@"png"];
    26. UIImage* img = [UIImage imageWithContentsOfFile:path];
    27. [detailbutton setBackgroundImage:img forState:UIControlStateNormal];
    28. [detailbutton setTitle:@"" forState:UIControlStateNormal];
    29. }
    30. if(indexPath.section == 1 && [self.model countOfSchedulingList]>0) //schedul
    31. {
    32. //recordbutton or cancelbutton
    33. UIButton* recordbutton = (UIButton*)[cell viewWithTag:5];
    34. [recordbutton addTarget:self action:@selector(cancelRecordEvent:) forControlEvents:UIControlEventTouchUpInside];
    35. [recordbutton setTitle:@"" forState:UIControlStateNormal];
    36. NSString* path = [[NSBundle mainBundle] pathForResource:@"button-delete_red" ofType:@"png"];
    37. UIImage* img = [UIImage imageWithContentsOfFile:path];
    38. [recordbutton setBackgroundImage:img forState:UIControlStateNormal];
    39. UIButton* button = (UIButton*)[cell viewWithTag:6];
    40. [button removeFromSuperview];
    41. }
    42. if(indexPath.section == 2 && [self.model countOfRecordedList]>0) //finished
    43. {
    44. //delete recordbutton
    45. UIButton* recordbutton = (UIButton*)[cell viewWithTag:5];
    46. [recordbutton removeFromSuperview];
    47. UIButton* deletbutton = (UIButton*) [cell viewWithTag:6];
    48. [deletbutton removeFromSuperview];
    49. //button tag:5
    50. if(event.isOnAir == FALSE)
    51. {
    52. UIButton* recbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    53. [recbutton addTarget:self action:@selector(playEvent:) forControlEvents:UIControlEventTouchUpInside];
    54. [recbutton setTitle:@"" forState:UIControlStateNormal];
    55. NSString* path = [[NSBundle mainBundle] pathForResource:@"button-play_red" ofType:@"png"];
    56. UIImage* img = [UIImage imageWithContentsOfFile:path];
    57. [recbutton setBackgroundImage:img forState:UIControlStateNormal];
    58. recbutton.frame = CGRectMake(270, 39, 30, 30);
    59. [cell addSubview:recbutton];
    60. }
    61. else if (event.isOnAir == TRUE)
    62. {
    63. UIButton* recbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    64. [recbutton addTarget:self action:@selector(pauseEvent:) forControlEvents:UIControlEventTouchUpInside];
    65. [recbutton setTitle:@"" forState:UIControlStateNormal];
    66. NSString* path = [[NSBundle mainBundle] pathForResource:@"button-pause_red" ofType:@"png"];
    67. UIImage* img = [UIImage imageWithContentsOfFile:path];
    68. [recbutton setBackgroundImage:img forState:UIControlStateNormal];
    69. recbutton.frame = CGRectMake(270, 39, 30, 30);
    70. [cell addSubview:recbutton];
    71. }
    72. //buton tag:6
    73. UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    74. [button addTarget:self action:@selector(deletionEvent:) forControlEvents:UIControlEventTouchUpInside];
    75. [button setTitle:@"" forState:UIControlStateNormal];
    76. NSString* path = [[NSBundle mainBundle] pathForResource:@"button-delete_red" ofType:@"png"];
    77. UIImage* img = [UIImage imageWithContentsOfFile:path];
    78. [button setBackgroundImage:img forState:UIControlStateNormal];
    79. button.frame = CGRectMake(232, 39, 30, 30);
    80. [cell addSubview:button];
    81. }
    82. event = nil;
    83. return cell;
    84. }
    Alles anzeigen


    Es war allerdings ein Krampf für Section 2 andere Buttons und Action einzufügen. Das Problem liegt genau da: Klicke ich auf einen Button (nr 5 oder 6) wird die richtige Methode aufgerufen. Zum Beispiel:

    Quellcode

    1. -(IBAction)playEvent:(id)sender
    2. {
    3. NSLog(@"play event");
    4. UITableViewCell *owningCell = (UITableViewCell*)[[sender superview]superview];
    5. //From the cell get its index path.
    6. NSIndexPath *indexPath = [self.tableView indexPathForCell:owningCell];
    7. BigEvent* event;
    8. event = [self.model.recordedList objectAtIndex:indexPath.row];
    9. fernbedienungAppDelegate* appDelegate = (fernbedienungAppDelegate*)[[UIApplication sharedApplication] delegate];
    10. [appDelegate playForID:event.eventID];
    11. event = nil;
    12. }
    Alles anzeigen

    Es wird immer der indexPath.row = 0 ausgegeben (sprich: ich bekomme immer das gleiche event)… Ich habe nach dem gleichem Schema auch in einer anderen TableView gearbeitet und dort funktioniert es. Das Problem wird jedoch bei den Buttons sein die ich da fleißig hinzufüge und entferne, denn das tue ich im anderen TabView nicht.

    Ich hoffe, jemand kann mir helfen.
    Vielen Dank,
    Juji
  • Warum speicherst Du die Zeilennummer nicht einfach im Tag des Buttons?

    Quellcode

    1. -(IBAction)playEvent:(id)sender
    2. {
    3. BigEvent* event = [self.model.recordedList objectAtIndex:sender.tag];
    4. fernbedienungAppDelegate* appDelegate = (fernbedienungAppDelegate*)[[UIApplication sharedApplication] delegate];
    5. [appDelegate playForID:event.eventID];
    6. event = nil;
    7. }
    „Meine Komplikation hatte eine Komplikation.“