简体   繁体   English

未调用 cellForRowAtIndexPath 但调用了 numberOfRowsInSection

[英]cellForRowAtIndexPath not called but numberOfRowsInSection called

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 .我有一个UIViewControler (称为RootController ),它加载一个包含tableViewUIView (称为SecondView )。 The problem is that the UIView call the numberOfSectionsInTableView and the numberOfRowsInSection but don't call cellForRowAtIndexPath and the table view is not displayed.问题是UIView调用numberOfSectionsInTableViewnumberOfRowsInSection但不调用cellForRowAtIndexPath并且不显示表格视图。 The code of the RootViewController is: 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 SecondView is: 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.谢谢你。

需要设置UITableView的Frame

This could also happen if reloadData is called on a different thread.如果在不同的线程上调用 reloadData,也可能发生这种情况。 Make sure it is run on the main thread since all UI stuff has to happen on the main thread.确保它在主线程上运行,因为所有 UI 内容都必须在主线程上发生。

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

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).从中获取“确保您的数据源/委托寿命长”的概念,但不要复制粘贴它,因为您还需要做其他事情(例如设置框架等)。

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 上的这个问题,首先要明确约束:

在此处输入图片说明

then apply constraints in following fashion:然后以下列方式应用约束:

在此处输入图片说明

You can only call the viewcontroller's view AFTER viewDidLoad is called.您只能在调用 viewDidLoad之后调用视图控制器的视图。 You can't interact with self.view in your init method您无法在 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).在您的情况下,您需要使用框架初始化您的 tableview(如上面代码中的建议)。 Just make sure you add the code in viewDidLoad in your viewController只需确保在 viewController 的 viewDidLoad 中添加代码

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

I have same issue like you:我有和你一样的问题:

I have only one method from delegate protocol that called and this is numberOfRowsInSection .我只有一种来自委托协议的方法调用,这是numberOfRowsInSection Meanwhile I have second method cellForRowAtIndexPath that was not called.同时我有第二种方法cellForRowAtIndexPath没有被调用。

The reason of this behaviour was that my numberOfRowsInSection returned 0 rows and cellForRowAtIndexPath didn't call because it needed to draw 0 cells.这种行为的原因是我的numberOfRowsInSection返回了 0 行并且cellForRowAtIndexPath没有调用,因为它需要绘制 0 个单元格。

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.我有完全相同的问题。 numberOfRowsInSection would fire, but cellForRowAt would not. numberOfRowsInSection会触发,但cellForRowAt不会。 I tore my code apart looking for the reason, but the reason is not in the code.我撕开我的代码寻找原因,但原因不在代码中。

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.选择表格视图,单击“添加新约束”图标(看起来像一个方形 TIE 战斗机)并为顶部、底部、前导和尾随设置约束。 Then cellForRowAt will be called and your table will populate.然后将调用cellForRowAt并填充您的表格。

I hope this helps someone.我希望这可以帮助别人。

This solution was specific for my case but maybe helps someone.此解决方案特定于我的情况,但可能对某人有所帮助。

I had the same problem.我有同样的问题。 I was using a computed property instead of stored property;我使用的是计算属性而不是存储属性; so every time I call the tableView, I was getting a new one.所以每次我调用 tableView 时,我都会得到一个新的。

I had this code:我有这个代码:

var tableView: UITableView{
    let tableView = UITableView()
    tableView.dataSource = self
    tableView.delegate = self
    tableView.register(SomeCell.self, forCellReuseIdentifier: cellID)
    return tableView
}

And this is the fixed code:这是固定代码:

lazy var tableView: UITableView = {
    let tableView = UITableView()
    tableView.dataSource = self
    tableView.delegate = self
    tableView.register(SomeCell.self, forCellReuseIdentifier: cellID)
    return tableView
}()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cellForRowAtIndexPath永远不会被调用,但是numberOfRowsInSection是 - cellForRowAtIndexPath never gets called, but numberOfRowsInSection is numberOfRowsInSection多次调用,但从未调用cellForRowAtIndexPath - numberOfRowsInSection called multiple times but cellForRowAtIndexPath never called 在reloadData之后没有调用cellForRowAtIndexPath但是numberOfRowsInSection没有调用 - cellForRowAtIndexPath not called after reloadData but numberOfRowsInSection does 当numberOfRowsInSection等于零时强制调用cellForRowAtIndexPath - Force cellForRowAtIndexPath to be called when numberOfRowsInSection equals to zero 未调用cellForRowAtIndexPath,但为UITableView调用的numberofRowsInSection已添加为UIView的子视图 - cellForRowAtIndexPath not called but numberofRowsInSection called for UITableView added as subview to UIView numberOfRowsInSection返回大于0的值; cellForRowAtIndexPath仍然没有被调用 - numberOfRowsInSection returns more than 0; cellForRowAtIndexPath still not being called 在比numberOfRowsInSection更晚调用之后,cellForRowAtIndexPath崩溃 - cellForRowAtIndexPath crashes after being called much later than numberOfRowsInSection 即使numberOfRowsInSection中的[objects count]显示46个对象,也不会调用cellForRowAtIndexPath - cellForRowAtIndexPath not called even when [objects count] in numberOfRowsInSection shows 46 objects numberOfRowsInSection 已调用但 cellForRowAt 未调用 - numberOfRowsInSection called but cellForRowAt not called iOS的cellForRowAtIndexPath没有被调用但是numberOfRowsInSection被调用并返回一个数字 - ios cellForRowAtIndexPath doesn't get called however numberOfRowsInSection gets called and returns a number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM