Po analizie danych JSON w klasie Data, ustawiam właściwość NSArray * nagłówków UIViewController w metodzie fillArrays tej samej klasy Data. W metodzie viewDidAppear mojego kontrolera UIViewController wywołuję reloadData na moim UITableView. numberOfSectionsInTableView uruchamia i zwraca 1, a następnie numberOfRowsInSection odpala i zwraca tablicę 4 (dla 4 ciągów w tablicy). Jednak kontrola nigdy nie trafia do cellForRowAtIndexPath i najtrudniej jest mi zrozumieć dlaczego, zwłaszcza, że mam prawidłowe sekcje i wiersze. Wszystkie komórki są widoczne.cellForRowAtIndexPath nie wywołane; sekcje zwraca 1, a wiersze zwraca 4
Dodałem protokoły UITableViewDataSource i UITableViewDelegate do interfejsu UIViewController i ustawę delegata UITableView i źródło danych na siebie w viewDidLoad (co również jest weryfikowane przez wywoływanie metod wiersza i sekcji liczenia).
Zastanawiam się, czy ma coś ze mną reinicjować UIViewController w Data.m, aby ustawić jego właściwości.
W Data.m:
- (void)fillArrays:(NSArray *)jsonObjs {
NSLog(@"fillArrays");
HeadlinesRootViewController *hrvc = [[HeadlinesRootViewController alloc] init];
hrvc.headlines = [self getJsonValuesForKey:@"headline" inArrayOfObjects:jsonObjs];
[hrvc viewDidAppear:NO];
}
W ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewDidLoad");
// Table view
headlineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
[headlineTableView setDelegate:self];
[headlineTableView setDataSource:self];
// Temporary
self.headlines = [[NSMutableArray alloc] initWithObjects:@"headline1", @"headline2", @"headline3", @"headline4", nil];
[self.view addSubview:headlineTableView];
self.headlineTableView = headlineTableView;
[headlineTableView release];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"viewdidappear");
NSLog(@"headlines: %@", self.headlines); // Returns an array of 4 headlines
if([self.headlines count] != 0){
[self.headlineTableView reloadData];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"numberOfSectionsInTableView: 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"numberOfRowsInSection: %d", [self.headlines count]);
return [self.headlines count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [[NSString alloc] initWithFormat:@"%@", [self.headlines objectAtIndex:indexPath.row]];
return cell;
}
użytkownik może chcieć opublikować kod – coneybeare