cellForRowAtIndexPath未被调用; sections返回1,rows返回4

azd*_*dev 5 iphone cocoa-touch objective-c uitableview

在解析Data类中的JSON数据之后,我在同一Data类的fillArrays方法中设置了UIViewController的NSArray*headlines属性.在我的UIViewController的viewDidAppear方法中,我在UITableView上调用reloadData.numberOfSectionsInTableView触发并返回1,然后numberOfRowsInSection触发并返回数组计数4(对于数组中的4个字符串).但是,控制永远不会到达cellForRowAtIndexPath,而我最难理解为什么,特别是因为我有有效的部分和行.细胞都是可见的.

我已经将UITableViewDataSource和UITableViewDelegate协议添加到UIViewController接口,并在ViewDidLoad中将UITableView的委托和dataSource设置为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];
}
Run Code Online (Sandbox Code Playgroud)

在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;
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*ner 4

在 fillArrays 中,您创建另一个视图控制器 - 但您永远不会对其或其视图执行任何操作,您永远不会看到该视图。您也永远不会手动调用 viewDidAppear,这会在显示视图控制器视图时自动发生(尽管仅在导航控制器的上下文中)。

通常的流程是,您创建一个视图控制器,然后将该视图添加为当前视图的子视图,或者通过导航控制器将其作为新窗口推送。我很确定您的整个问题是它们的表从未添加到任何人实际看到的视图中,因此该表调用其他方法但从不调用 cellForRow 因为它的layoutSubviews代码根本没有被调用。