ios 未调用 cellForRowAtIndexPath 但调用了 numberOfRowsInSection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21434161/
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-08-30 23:18:22  来源:igfitidea点击:

cellForRowAtIndexPath not called but numberOfRowsInSection called

iosobjective-cuitableview

提问by Anna565

I have a simple problem but I can't understand what is. I have a UIViewControler(called RootController) that load a UIView(called SecondView) that contain a tableView. The problem is that the UIViewcall the numberOfSectionsInTableViewand the numberOfRowsInSectionbut don't call cellForRowAtIndexPathand the table view is not displayed. The code of the RootViewControlleris:

我有一个简单的问题,但我不明白是什么。我有一个UIViewControler(称为RootController)加载一个UIView(称为SecondView),其中包含一个tableView. 问题是UIView调用numberOfSectionsInTableViewnumberOfRowsInSection但不调用cellForRowAtIndexPath并且不显示表视图。的代码RootViewController是:

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];

And the code of the SecondViewis:

的代码SecondView是:

@interface SecondView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,retain) UITableView *table;
@end

@implementation SecondView
@synthesize table;

- (id)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
   if (self) {
   self.table = [[UITableView alloc] init];
   self.table.delegate = self;
   self.table.dataSource = self;
   [self addSubview:self.table];
   }
 return self;
}

- (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];
     }
  cell.textLabel.text = @"Prova";
  return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return 5;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 1;
}

Can you help me to find the problem? Thank you.

你能帮我找出问题吗?谢谢你。

回答by Hussain Shabbir

You need to set the Frame of UITableView

您需要设置Frame UITableView

回答by spfursich

This could also happen if reloadData is called on a different thread. Make sure it is run on the main thread since all UI stuff has to happen on the main thread.

如果在不同的线程上调用 reloadData,也可能发生这种情况。确保它在主线程上运行,因为所有 UI 内容都必须在主线程上发生。

dispatch_async(dispatch_get_main_queue(),{
            self.myTableView.reloadData()
        });

回答by ArtHare

My problem was that I had a simple class doing the implementing of my delegate and data source, but the lifetime of the simple class was too short.

我的问题是我有一个简单的类来实现我的委托和数据源,但是这个简单类的生命周期太短了。

I was doing

我在做

MyDataSourceClass* myClass = [[MyDataSourceClass alloc] initWithNSArray:someArray];
tableView.dataSource = self.tableViewDataSource;
tableView.delegate = self.tableViewDataSource;
[tableView reloadData];

// end of function, myClass goes out of scope, and apparently tableView has a weak reference to it

Needed to be doing

需要做的

self.tableDataSource = [[MyDataSourceClass alloc] initWithNSArray:someArray];
tableView.dataSource = myClass;
tableView.delegate = myClass;
[tableView reloadData];
// now at the end of the function, tableDataSource is still alive, and the tableView will be able to query it.

Note that the code above is pseudocode from memory. Take from it the concept of "make sure your data source/delegate lives long", but don't copy paste it, because there's other stuff you need to do (like set your frame etc etc).

请注意,上面的代码是来自内存的伪代码。从中获取“确保您的数据源/委托寿命长”的概念,但不要复制粘贴它,因为您还需要做其他事情(例如设置框架等)。

回答by Aqib Mumtaz

In XCode6 same weird problem was occurring to me when "Add Missing Constraints" is applied on tableView on Storyboard to adjust views.

在 XCode6 中,当在 Storyboard 上的 tableView 上应用“添加缺少的约束”以调整视图时,我遇到了同样奇怪的问题。

To resolve this issue on Storyboard first clear constraints:

要解决 Storyboard 上的这个问题,首先要明确约束:

enter image description here

在此处输入图片说明

then apply constraints in following fashion:

然后以下列方式应用约束:

enter image description here

在此处输入图片说明

回答by Thomas Keuleers

You can only call the viewcontroller's view AFTERviewDidLoad is called. You can't interact with self.view in your init method

您只能在调用viewDidLoad之后调用视图控制器的视图。您无法在 init 方法中与 self.view 交互

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self addSubview:self.table];
}

In your case, you need to init your tableview with a frame (like a suggested in the code above). Just make sure you add the code in viewDidLoad in your viewController

在您的情况下,您需要使用框架初始化您的 tableview(如上面代码中的建议)。只需确保在 viewController 的 viewDidLoad 中添加代码

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60,     self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];

回答by wm.p1us

I have same issue like you:

我有和你一样的问题:

I have only one method from delegate protocol that called and this is numberOfRowsInSection. Meanwhile I have second method cellForRowAtIndexPaththat was not called.

我只有一种来自委托协议的方法调用,这是numberOfRowsInSection。同时我有第二种方法cellForRowAtIndexPath没有被调用。

The reason of this behaviour was that my numberOfRowsInSectionreturned 0 rows and cellForRowAtIndexPathdidn't call because it needed to draw 0 cells.

这种行为的原因是我的numberOfRowsInSection返回了 0 行并且cellForRowAtIndexPath没有调用,因为它需要绘制 0 个单元格。

回答by ttt

I would comment on Hussain Shabbir's answer, to clarify it, but as I am not yet able to comment, I will post an answer instead.

我会评论 Hussain Shabbir 的回答,以澄清它,但由于我还不能发表评论,我会发布一个答案。

I had exactly the same issue. numberOfRowsInSectionwould fire, but cellForRowAtwould not. I tore my code apart looking for the reason, but the reason is not in the code.

我有完全相同的问题。numberOfRowsInSection会火,但cellForRowAt不会。我撕开我的代码寻找原因,但原因不在代码中。

The solution (for me) was in the storyboard. I had not set constraints for the Table View. Select the table view, click the "Add New Constraints" icon(looks like a square TIE fighter) and set constraints for top, bottm, leading and trailing. Then cellForRowAtwill be called and your table will populate.

解决方案(对我来说)在故事板中。我没有为表视图设置约束。选择表格视图,单击“添加新约束”图标(看起来像一个方形 TIE 战斗机)并为顶部、底部、前导和尾随设置约束。然后cellForRowAt将被调用并填充您的表。

I hope this helps someone.

我希望这可以帮助别人。

回答by Vishal Chaudhari

Delete UITableView and add again in your ViewController

删除 UITableView 并在您的 ViewController 中再次添加