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

标签 iphone objective-c cocoa-touch uitableview

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

我已将 UITableViewDataSource 和 UITableViewDelegate 协议(protocol)添加到 UIViewController 接口(interface),并在 viewDidLoad 中将 UITableView 的委托(delegate)和数据源设置为自身(这也通过调用的行和节计数方法进行验证)。

我想知道它是否与我重新初始化 Data.m 中的 UIViewController 以设置其属性有关。

在数据中:

- (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;
}

最佳答案

在 fillArrays 中,您创建了另一个 View Controller - 但您永远不会对它或它的 View 做任何事情,您永远不会看到那个 View 。您也永远不会手动调用 viewDidAppear,这会在显示 View Controller View 时自动发生(尽管仅在导航 Controller 的上下文中)。

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

关于iphone - 未调用 cellForRowAtIndexPath; sections 返回 1,rows 返回 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1607915/

相关文章:

iphone - UIGraphicsBeginImageContext 线程安全吗?

iphone - 用 CoreData 对象填充 NSSet

objective-c - Xcode 中的位码签名失败

iphone - CAGradientLayer 中的 CABasicAnimation 不起作用 - 我做错了什么?

ios - "absolute position" ScrollView 内的 View

javascript - 为什么我的 iPhone Web 应用程序中两次被要求使用 GPS

iphone - - TableView :cellForRowAtIndexPath: is not getting called

Objective-c:使用带有导航栏的第二个 View 启动应用程序

ios - 如何使用 NSNumberFormatter 将数字格式化为有限字符的 NSString?

ios - NSNotificationCentre 崩溃