UITableView erstellen

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

  • UITableView erstellen

    Hallo OSX Entwickler,

    die Frage klingt vielleicht ein bisschen blöd, aber wie kann ich ein einfaches UITableView erstellen?

    Ich habe es so versucht:
    - Neues Projekt mit Template Master-Detail
    - In die Methode numberOfRowsInSection schreibe ich "return 2;"
    - In die Methode cellForRowAtIndexPath schreibe ich "cell.textLabel.text = [NSString stringWithFormat:@"Zeile %@", indexPath.row];"

    Es sollte eigentlich eine Tabelle mit 2 Zeilen erstellt werden, aber es kommt diese Fehlermeldung:

    Quellcode

    1. *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
    2. *** First throw call stack:
    3. (0xf99b02 0x1130cce 0xf864d4 0x444b3e 0x2415d6 0x1ee69b 0x1f0819 0xa3912 0xa340f 0xa639c 0xaa5f2 0x54e7b 0xf9b912 0x1aadd59 0x1ab7b6b 0x1a3ed91 0x1a40c90 0x1acb661 0x1452e 0x149e8 0x234c6 0x23f34 0x1790c 0x1395876 0xf6cff5 0xed1902 0xed01ea 0xecf694 0xecf5ab 0x14326 0x15851 0x2852 0x27c5 0x1)
    4. terminate called throwing an exception


    Was, bzw. wo ist der Fehler?
  • Mh seltsam, wenn ich ein neues Projekt anlege und mein MasterViewController folgenden Code hat

    Quellcode

    1. //
    2. // MasterViewController.m
    3. // test
    4. //
    5. // Created by Matthias Ludwig on 24.10.11.
    6. // Copyright (c) 2011 __MyCompanyName__. All rights reserved.
    7. //
    8. #import "MasterViewController.h"
    9. #import "DetailViewController.h"
    10. @implementation MasterViewController
    11. @synthesize detailViewController = _detailViewController;
    12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    13. {
    14. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    15. if (self) {
    16. self.title = NSLocalizedString(@"Master", @"Master");
    17. }
    18. return self;
    19. }
    20. - (void)dealloc
    21. {
    22. [_detailViewController release];
    23. [super dealloc];
    24. }
    25. - (void)didReceiveMemoryWarning
    26. {
    27. [super didReceiveMemoryWarning];
    28. // Release any cached data, images, etc that aren't in use.
    29. }
    30. #pragma mark - View lifecycle
    31. - (void)viewDidLoad
    32. {
    33. [super viewDidLoad];
    34. // Do any additional setup after loading the view, typically from a nib.
    35. }
    36. - (void)viewDidUnload
    37. {
    38. [super viewDidUnload];
    39. // Release any retained subviews of the main view.
    40. // e.g. self.myOutlet = nil;
    41. }
    42. - (void)viewWillAppear:(BOOL)animated
    43. {
    44. [super viewWillAppear:animated];
    45. }
    46. - (void)viewDidAppear:(BOOL)animated
    47. {
    48. [super viewDidAppear:animated];
    49. }
    50. - (void)viewWillDisappear:(BOOL)animated
    51. {
    52. [super viewWillDisappear:animated];
    53. }
    54. - (void)viewDidDisappear:(BOOL)animated
    55. {
    56. [super viewDidDisappear:animated];
    57. }
    58. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    59. {
    60. // Return YES for supported orientations
    61. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    62. }
    63. // Customize the number of sections in the table view.
    64. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    65. {
    66. return 1;
    67. }
    68. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    69. {
    70. return 2;
    71. }
    72. // Customize the appearance of table view cells.
    73. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    74. {
    75. static NSString *CellIdentifier = @"Cell";
    76. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    77. if (cell == nil) {
    78. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    79. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    80. }
    81. // Configure the cell.
    82. cell.textLabel.text = [NSString stringWithFormat:@"Zeile %d", indexPath.row];
    83. return cell;
    84. }
    85. /*
    86. // Override to support conditional editing of the table view.
    87. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    88. {
    89. // Return NO if you do not want the specified item to be editable.
    90. return YES;
    91. }
    92. */
    93. /*
    94. // Override to support editing the table view.
    95. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    96. {
    97. if (editingStyle == UITableViewCellEditingStyleDelete) {
    98. // Delete the row from the data source.
    99. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    100. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
    101. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    102. }
    103. }
    104. */
    105. /*
    106. // Override to support rearranging the table view.
    107. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    108. {
    109. }
    110. */
    111. /*
    112. // Override to support conditional rearranging of the table view.
    113. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    114. {
    115. // Return NO if you do not want the item to be re-orderable.
    116. return YES;
    117. }
    118. */
    119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    120. {
    121. if (!self.detailViewController) {
    122. self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
    123. }
    124. [self.navigationController pushViewController:self.detailViewController animated:YES];
    125. }
    126. @end
    Alles anzeigen


    funktioniert es und in den Beiden Zellen steht dann "Zeile 0", "Zeile1"