解析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