【发布时间】:2010-12-09 03:19:34
【问题描述】:
在 Data 类中解析 JSON 数据后,我在同一个 Data 类的 fillArrays 方法中设置了 UIViewController 的 NSArray *headlines 属性。在我的 UIViewController 的 viewDidAppear 方法中,我在我的 UITableView 上调用了 reloadData。 numberOfSectionsInTableView 触发并返回 1,然后 numberOfRowsInSection 触发并返回数组计数 4(对于数组中的 4 个字符串)。但是,控制永远不会到达 cellForRowAtIndexPath 并且我很难理解为什么,特别是因为我有有效的部分和行。单元格全部可见。
我已将 UITableViewDataSource 和 UITableViewDelegate 协议添加到 UIViewController 接口,并在 viewDidLoad 中将 UITableView 的委托和数据源设置为 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;
}
【问题讨论】:
-
你可能想发布一些代码
标签: iphone objective-c cocoa-touch uitableview