解析Data類中的JSON數據後,我在同一Data類的fillArrays方法中設置了UIViewController的NSArray *標題屬性。在我的UIViewController的viewDidAppear方法中,我在我的UITableView上調用reloadData。 numberOfSectionsInTableView觸發並返回1,則numberOfRowsInSection觸發並返回4的數組數(對於數組中的4個字符串)。然而,控制永遠不會到cellForRowAtIndexPath,我最難理解爲什麼,特別是因爲我有有效的部分和行。細胞都是可見的。cellForRowAtIndexPath not called;部分返回1,行返回4
我已經將UITableViewDataSource和UITableViewDelegate協議添加到UIViewController接口,並將UITableView的委託和數據源設置爲viewDidLoad中的self(它也通過調用的行和段計數方法進行驗證)。
我想知道它是否與我重新初始化Data.m中的UIViewController以設置其屬性。
在Data.m:
- (void)fillArrays:(NSArray *)jsonObjs {
NSLog(@"fillArrays");
HeadlinesRootViewController *hrvc = [[HeadlinesRootViewController alloc] init];
hrvc.headlines = [self getJsonValuesForKey:@"headline" inArrayOfObjects:jsonObjs];
[hrvc viewDidAppear:NO];
}
在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;
}
你可能想要發表一些代碼 – coneybeare 2009-10-22 15:44:12