xcode 未调用 cellForRowAtIndexPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8352500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:38:57  来源:igfitidea点击:

cellForRowAtIndexPath not being called

objective-cxcodeuitableview

提问by Batnom

I have a view that adds another view on top in this manner:

我有一个视图,以这种方式在顶部添加另一个视图:

- (void)showAreaEditView {

    NSLog(@"SHOWING AREA EDITOR VIEW");

    if (self.thisAreaEditorView == nil) {

        // Create View
        AreaEditorView *tmpViewController = [[AreaEditorView alloc] initWithNibName:@"AreaEditorView" bundle:nil];
        self.thisAreaEditorView = tmpViewController;
        [tmpViewController release];

        // Hide the back button
        self.thisAreaEditorView.navigationItem.hidesBackButton = YES;

    }

    self.thisAreaEditorView.myInspectionID = self.myInspectionID;
    self.thisAreaEditorView.loggedIn = loggedIn;
    self.thisAreaEditorView.loggedInGroup = loggedInGroup;

    // Slide view up

    [self.view addSubview:thisAreaEditorView.view];


    CGRect endFrame = CGRectMake(self.view.frame.size.width/2 - thisAreaEditorView.view.frame.size.width/2,
                                 self.view.frame.size.height/2 - thisAreaEditorView.view.frame.size.height/2, 
                                 thisAreaEditorView.view.frame.size.width, 
                                 thisAreaEditorView.view.frame.size.height);

    CGRect startFrame = endFrame; // offscreen source

    // new view starts off bottom of screen
    startFrame.origin.y += self.view.frame.size.height;
    self.thisAreaEditorView.view.frame = startFrame;

    // start the slide up animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.6];   
    thisAreaEditorView.view.frame = endFrame; // slide in
    [UIView commitAnimations];

}

I'm sure you can just ignore the slide part, I feel the addSubview is relevant.

我确定您可以忽略幻灯片部分,我觉得 addSubview 是相关的。

Then in thisAreaEditor I have the view with the table and buttons and such. UITableView delegate/datasource is going to File's Owner as normal.

然后在 thisAreaEditor 中,我拥有带有表格和按钮等的视图。UITableView 委托/数据源将照常发送给文件所有者。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"numberOfRowsInSection returning %d", [tableData count]);
    [tableData count];

}

This function numberOfRowsInSection returns 4

此函数 numberOfRowsInSection 返回 4

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    NSString *thisText =  [tableData objectAtIndex:indexPath.row];
    cell.textLabel.text = thisText;

    NSLog(@"looking at cell %d text:%@", indexPath.row, thisText);

    return cell;

}

But cellForRowAtIndexPath never gets called.

但是 cellForRowAtIndexPath 永远不会被调用。

I'm at a loss here, I have no idea how it can seem to work fine but one of the delegate functions simply not be called.

我在这里不知所措,我不知道它是如何正常工作的,但其中一个委托函数根本没有被调用。

I have tried [bigTable reloadData]and so on, the table just never gets populated and no logs from the function output.

我已经尝试过[bigTable reloadData]等等,该表永远不会被填充并且没有来自函数输出的日志。

Thanks in advance.

提前致谢。

回答by NJones

You might have just edited this out, if so I'm sorry, but it looks like you forgot to return tableData's count.

您可能刚刚编辑了它,如果是这样,我很抱歉,但您似乎忘记返回 tableData 的计数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? NSLog(@"numberOfRowsInSection returning %d", [tableData count]);
? ? return [tableData count];
}

回答by hellozimi

It seems you're missing UITableViewDelegate.

看来您缺少 UITableViewDelegate。

If you're using Interface Builder, right click the table view outlet and drag both delegateand datasourceto File's Owner.

如果您使用界面生成器中,右键单击表格视图出口和阻力都delegatedatasourceFile's Owner

And if not using Interface Builder add this where you init your tableView

如果不使用 Interface Builder,请在您初始化 tableView 的地方添加它

bigTable.delegate = self;
bigTable.dataSource = self;

Remember to import the UITableViewDelegate and UITableViewDataSource protocols, just as Srikar says.

记住导入 UITableViewDelegate 和 UITableViewDataSource 协议,就像 Srikar 所说的那样。

Hope this is to any help.

希望这对任何帮助。

Cheers!

干杯!

回答by Bijoy Thangaraj

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathis also not called when the tableview's height is not set.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath未设置 tableview 的高度时也不会调用。

回答by Srikar Appalaraju

Maybe you did not set the tableView delegate as self or the datasource as self. Add this code & see if it works now -

也许您没有将 tableView 委托设置为 self 或将数据源设置为 self。添加此代码并查看它现在是否有效 -

[tableView setDelegate:self];
[tableView setDataSource:self];

Also in your header file inherit these delegates - UITableViewDelegate, UITableViewDataSource

同样在您的头文件中继承这些委托 - UITableViewDelegate, UITableViewDataSource

@interface yourViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>

Hope this helps.

希望这可以帮助。

回答by verma

This is a older link, but I wanted to update this with putting info on how I resolved this issue. For me the issue was the Array to populate the table had 0 rows so cellForRowAtIndexPath was never called. Make sure that the Array you are using to populate the table has data in it.

这是一个较旧的链接,但我想通过提供有关我如何解决此问题的信息来更新此链接。对我来说,问题是用于填充表的 Array 有 0 行,因此从未调用 cellForRowAtIndexPath。确保用于填充表的数组中包含数据。