繁体   English   中英

未调用 cellForRowAtIndexPath 但调用了 numberOfRowsInSection

[英]cellForRowAtIndexPath not called but numberOfRowsInSection called

我有一个简单的问题,但我不明白是什么。 我有一个UIViewControler (称为RootController ),它加载一个包含tableViewUIView (称为SecondView )。 问题是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];

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

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

需要设置UITableView的Frame

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

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

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

我在做

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

需要做的

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.

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

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

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

在此处输入图片说明

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

在此处输入图片说明

您只能在调用 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];
}

在您的情况下,您需要使用框架初始化您的 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];

我有和你一样的问题:

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

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

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

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

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

我希望这可以帮助别人。

此解决方案特定于我的情况,但可能对某人有所帮助。

我有同样的问题。 我使用的是计算属性而不是存储属性; 所以每次我调用 tableView 时,我都会得到一个新的。

我有这个代码:

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

这是固定代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM