Sections in Uitableview

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

  • Sections in Uitableview

    Hallo an Alle,

    ich möchte gerne eine Uitableview mit Sections erstellen.

    Die Daten werden aus einer Property List gelesen.

    Ich habe 3 Sections mit jeweils 2 Zeilen. Das ganze wird auch so dargestellt.
    Nur leider beginnt jede Section mit dem gleichen Eintrag, d.h. es wird wohl immer wieder von Anfang gezählt.
    (Jede Sections hat den gleichen Inhalt)

    Bestimmt hat das Problem mit meiner Property List zu tun. Diese ist nicht in die Sections unterteilt.

    Oder kann man vielleicht sagen, das in der zweiten Section (section == 1) erst ab dem dritten Eintrag ( Item 2 ) aus der Property List gezählt werden soll?

    Bin erst Anfänger und habe mich bereits Tage mit diesen Thema beschäftigt. Kriege es aber einfach nicht hin.
    Für Eure Hilfe wäre ich sehr dankbar!

    Viele Grüße
    Bettina

    Hier meine Tableview

    Quellcode

    1. @interface PictureTableViewController ()
    2. @property (nonatomic, strong) NSArray *pictureArray;
    3. @end
    4. @implementation PictureTableViewController
    5. @synthesize pictureArray = _pictureArray;
    6. - (id)initWithStyle:(UITableViewStyle)style
    7. {
    8. self = [super initWithStyle:style];
    9. if (self) {
    10. // Custom initialization
    11. }
    12. return self;
    13. }
    14. - (void)didReceiveMemoryWarning
    15. {
    16. [super didReceiveMemoryWarning];
    17. }
    18. #pragma mark - View lifecycle
    19. - (void)viewDidLoad
    20. {
    21. [super viewDidLoad];
    22. self.pictureArray = [[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]
    23. pathForResource:@"Bilder" ofType:@"plist"]] objectForKey:@"pictures"];
    24. }
    25. - (void)viewDidUnload
    26. {
    27. [super viewDidUnload];
    28. - (void)viewWillAppear:(BOOL)animated
    29. {
    30. [super viewWillAppear:animated];
    31. }
    32. - (void)viewDidAppear:(BOOL)animated
    33. {
    34. [super viewDidAppear:animated];
    35. }
    36. - (void)viewWillDisappear:(BOOL)animated
    37. {
    38. [super viewWillDisappear:animated];
    39. }
    40. - (void)viewDidDisappear:(BOOL)animated
    41. {
    42. [super viewDidDisappear:animated];
    43. }
    44. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    45. {
    46. }
    47. #pragma mark - Table view data source
    48. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    49. {
    50. // Return the number of sections.
    51. return 3;
    52. }
    53. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    54. {
    55. if (section == 0) { // Lautsprecher....
    56. return 2;
    57. } else if (section == 1) { // Soundkarten
    58. return 2;
    59. }else if (section == 2) { // speicher
    60. return 2;
    61. }
    62. return 0;
    63. // Return the number of rows in the section.
    64. return [self.pictureArray count];
    65. }
    66. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    67. {
    68. if (section == 0) { // Lautsprecher....
    69. return @"Lautsprecher";
    70. } else if (section == 1) { // Soundkarten
    71. return @"Soundkarten";
    72. }else if (section == 2) { // speicher
    73. return @"Speicher";
    74. }
    75. return @"";
    76. }
    77. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    78. {
    79. static NSString *CellIdentifier = @"pictureCell";
    80. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    81. // Configure the cell...
    82. NSDictionary *dict = [self.pictureArray objectAtIndex:indexPath.row];
    83. cell.imageView.image = [UIImage imageNamed:[dict objectForKey:@"filename"]];
    84. cell.textLabel.text = [dict objectForKey:@"description"];
    85. cell.detailTextLabel.text = [dict objectForKey:@"subtitle"];
    86. return cell;
    87. }
    Alles anzeigen


    Hier meine Property List

    XML-Quellcode

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <plist version="1.0">
    4. <dict>
    5. <key>pictures</key>
    6. <array>
    7. <dict>
    8. <key>subtitle</key>
    9. <string>text1</string>
    10. <key>filename</key>
    11. <string>audio-card-2.png</string>
    12. <key>description</key>
    13. <string>Ein Lautsprecher1</string>
    14. </dict>
    15. <dict>
    16. <key>subtitle</key>
    17. <string>text2</string>
    18. <key>filename</key>
    19. <string>audio-card-3.png</string>
    20. <key>description</key>
    21. <string>Ein Lautsprecher 2</string>
    22. </dict>
    23. <dict>
    24. <key>subtitle</key>
    25. <string>text3</string>
    26. <key>filename</key>
    27. <string>audio-card-4.png</string>
    28. <key>description</key>
    29. <string>Eine einfache Soundkarte1</string>
    30. </dict>
    31. <dict>
    32. <key>subtitle</key>
    33. <string>text4</string>
    34. <key>filename</key>
    35. <string>audio-card-4.png</string>
    36. <key>description</key>
    37. <string>Eine einfache Soundkarte2</string>
    38. </dict>
    39. <dict>
    40. <key>subtitle</key>
    41. <string>text5</string>
    42. <key>filename</key>
    43. <string>audio-card-4.png</string>
    44. <key>description</key>
    45. <string>Ein toller Speicher 2</string>
    46. </dict>
    47. <dict>
    48. <key>subtitle</key>
    49. <string>text5</string>
    50. <key>filename</key>
    51. <string>audio-card-4.png</string>
    52. <key>description</key>
    53. <string>Ein toller Speicher 1</string>
    54. </dict>
    55. </array>
    56. </dict>
    57. </plist>
    Alles anzeigen


    Hat jemand eine Idee
  • Ja.
    Du solltest das Modell entsprechend dem Sectioning umbauen.
    Vergleiche dazu: uiswitch in uitableviewcell

    Ansonsten wird, wie du schon sagtest, immer von 0 angefangen.
    In deinem Fall geht es vermutlich so:

    C-Quellcode

    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    2. {
    3. static NSString *CellIdentifier = @"pictureCell";
    4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    5. // Configure the cell...
    6. NSDictionary *dict = [self.pictureArray objectAtIndex:(indexPath.section*2)+indexPath.row];
    7. cell.imageView.image = [UIImage imageNamed:[dict objectForKey:@"filename"]];
    8. cell.textLabel.text = [dict objectForKey:@"description"];
    9. cell.detailTextLabel.text = [dict objectForKey:@"subtitle"];
    10. return cell;
    11. }
    Alles anzeigen


    Da jede Section ja angeblich immer zwei Rows hat, kannst du das so ziemlich statisch abfangen und einfach die auszulassenden Indices dazuaddieren.
    Sauber finde ich das aber nun wirklich nicht. ;)
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • Noch ein kleines Problem ...

    Vielen lieben Dank.
    Das funktioniert schon mal super!!!!
    Klar, ist das nicht sauberste Lösung, aber in diesem Fall funktioniert es.
    Muss mich wirklich noch eingehend mit diesem Thema beschäftigen.

    Ich habe noch eine DetailView wenn man die Zeile auswählt.
    Die zeigt, aber nun für die zweite Section leider die Infos aus der ersten Section an ....
    Kann ich das auch irgendwie anpassen ?

    Quellcode

    1. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    2. {
    3. if ([segue.identifier isEqualToString:@"showPicture"]) {
    4. PictureViewController *pvc = segue.destinationViewController;
    5. pvc.pictureDictionary = [self.pictureArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];
    6. }
    7. }


    Wenn das noch funktionieren würde, wäre ich super happy !!!
  • Gut, mit Storyboards kenn ich mich nun echt nicht aus.
    Offenbar musst du auch da den Index anpassen.

    Also statt

    C-Quellcode

    1. pvc.pictureDictionary = [self.pictureArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];


    lieber ein

    C-Quellcode

    1. NSIndexPath * currentIndexPat = [self.tableView indexPathForSelectedRow];
    2. pvc.pictureDictionary = [self.pictureArray objectAtIndex:(currentIndexPath.section*2)+currentIndexPath.row];


    Du hast ja dasselbe Problem hier: indexPath.row gibt die Reihe in der aktuellen Section aus, und die beginnt je Section bei 0.
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P