UITableview mit 6 Sections und variabler Anzahl von einträgen

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

  • Wenn du in dem Dictionary jeweils Arrays hast dann kannst du einfach auf das Array der entsprechenden section rauskramem
    Mit ibjectForKey und dann auf das Array
    Count schicken und dann haste ja die Anzahl der Rows pro Section. Du solltest mal genau deine Datenstruktur mitteilen :)
    Gruß

    Robin
  • So..

    habe nun evrschidene Sections, die je nach Vorhandenheit der einträge erzeugt werden, oder bene auch nicht. Problem ist nun folgendes:

    ich habe in dem array drei einträge, es werden aber nur 2 sections angelegt.

    Code vom TableViewController:

    Quellcode

    1. #pragma mark - tableView
    2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    3. {
    4. // Return the number of sections.
    5. Component *component = [[[Component alloc] init] autorelease];
    6. NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    7. array = [component seperateComponentsIntoArrayWithTypes:componentArray];
    8. NSLog(@"%i", [array count]);
    9. return [array count];
    10. }
    11. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    12. {
    13. Component *component = [[[Component alloc] init] autorelease];
    14. NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    15. array = [component seperateComponentsIntoArrayWithTypes:componentArray];
    16. if (section == 1) {
    17. NSMutableArray *typeArray = [[[NSMutableArray alloc] init] autorelease];
    18. typeArray = [array objectAtIndex:section];
    19. return [typeArray count];
    20. }
    21. else if(section == 2) {
    22. NSMutableArray *typeArray = [[[NSMutableArray alloc] init] autorelease];
    23. typeArray = [array objectAtIndex:section];
    24. return [typeArray count];
    25. }
    26. else if(section == 3) {
    27. NSMutableArray *typeArray = [[[NSMutableArray alloc] init] autorelease];
    28. typeArray = [array objectAtIndex:section];
    29. return [typeArray count];
    30. }
    31. else if(section == 4) {
    32. NSMutableArray *typeArray = [[[NSMutableArray alloc] init] autorelease];
    33. typeArray = [array objectAtIndex:section];
    34. return [typeArray count];
    35. }
    36. else if(section == 5) {
    37. NSMutableArray *typeArray = [[[NSMutableArray alloc] init] autorelease];
    38. typeArray = [array objectAtIndex:section];
    39. return [typeArray count];
    40. }
    41. else if(section == 6) {
    42. NSMutableArray *typeArray = [[[NSMutableArray alloc] init] autorelease];
    43. typeArray = [array objectAtIndex:section];
    44. return [typeArray count];
    45. }
    46. return 0;
    47. }
    48. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    49. {
    50. // The header for the section is the region name -- get this from the region at the section inde
    51. if (section == 1) {
    52. return [types objectAtIndex:0];
    53. }
    54. else if(section == 2) {
    55. return [types objectAtIndex:1];
    56. }
    57. else if(section == 3) {
    58. return [types objectAtIndex:2];
    59. }
    60. else if(section == 4) {
    61. return [types objectAtIndex:3];
    62. }
    63. else if(section == 5) {
    64. return [types objectAtIndex:4];
    65. }
    66. else if(section == 6) {
    67. return [types objectAtIndex:5];
    68. }
    69. else {
    70. return nil;
    71. }
    72. }
    73. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    74. Component *component = [[[Component alloc] init] autorelease];
    75. static NSString *CellIdentifier = @"ComponentCell";
    76. ComponentCell_CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    77. if (cell == nil) {
    78. cell = [[[ComponentCell_CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    79. }
    80. NSMutableArray *array = [dict objectForKey:[types objectAtIndex:indexPath.section-1]];
    81. component = [array objectAtIndex:indexPath.row];
    82. [cell setDescribtion:component.componentDescribtion];
    83. [cell setType:component.componentType];
    84. [cell setSurfaceSize:component.surfaceSize];
    85. return cell;
    86. }
    Alles anzeigen


    hoffe hier kann mir jemand helfen :)


    EDIT: Hat sich erledigt, hab nicht bedacht, das section nullbasiert ist :)
    Mein Blog: danielbocksteger.wordpress.com

    schaut mal vorbei :)

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von DBocksteger_04 ()

  • Evtl. solltest Du Dich evtl. auch mal mehr mit den Basics beschäftigen.

    Es ist weder sinnvoll noch effektiv z.B. das Objekt Component bei jeder Verwendung neu zu erzeugen und direkt wieder zu verwerfen!

    Weiterhin kannst Du Dir in der Methode tableView:numberOfRowsInSection: nicht einfach das Array mit den Daten zu einer Section in der Variable typeArray merken und dann in der Methode tableView:titleForHeaderInSection: auf dieses Array über typeArray zugreifen. Es wird von Apple bzw. iOS nicht garantiert, dass die Methode tableView:numberOfRowsInSection: immer direkt vor der Methode tableView:titleForHeaderInSection: und dann auch noch für die gleiche Section aufgerufen wird. Beide Methode müssen unabhängig voneinander vom iOS aufgerufen werden können.