Ios cellForRowAtIndexPath未调用,但numberOfRowsInSection已调用
我有一个简单的问题,但我不明白是什么。 我有一个Ios cellForRowAtIndexPath未调用,但numberOfRowsInSection已调用,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个简单的问题,但我不明白是什么。 我有一个uiviewcontroller(称为RootController),它加载一个UIView(称为SecondView),其中包含一个tableView。问题是,ui视图调用numberofsectionsintable视图和numberofrowsin节,但不调用cellforrowtaindexpath,并且不显示表视图。 RootViewController的代码为: SecondView *secondView = [[seconddVie
uiviewcontroller
(称为RootController
),它加载一个UIView
(称为SecondView
),其中包含一个tableView
。问题是,ui视图
调用numberofsectionsintable视图
和numberofrowsin节
,但不调用cellforrowtaindexpath
,并且不显示表视图。
RootViewController
的代码为:
SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: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;
}
而SecondView
的代码是:
SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: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*表;
@结束
@实现第二视图
@综合表;
-(id)initWithFrame:(CGRect)帧{
self=[super initWithFrame:frame];
如果(自我){
self.table=[[UITableView alloc]init];
self.table.delegate=self;
self.table.dataSource=self;
[self addSubview:self.table];
}
回归自我;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textlab.text=@“Prova”;
返回单元;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回5;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
你能帮我找到问题吗?谢谢。您需要设置
UITableView
的框架,您只能在调用viewDidLoad后调用viewcontroller的视图。
无法在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(就像上面代码中建议的那样)。只需确保将viewDidLoad中的代码添加到viewController中即可
SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];
在XCode6中,当“添加缺少的约束”应用于情节提要上的tableView以调整视图时,我也遇到了同样的奇怪问题 要解决故事板上的此问题,请首先清除约束: 然后按以下方式应用约束:
如果在不同的线程上调用重载数据,也可能发生这种情况。确保它在主线程上运行,因为所有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.
请注意,上面的代码是来自内存的伪代码。从中吸取“确保数据源/代理寿命长”的概念,但不要复制粘贴它,因为还有其他事情需要做(如设置帧等)。删除UITableView并在ViewController中再次添加我和你有同样的问题:
SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];
我只有一个从委托协议调用的方法,它是numberofrowsinssection。同时,我还有第二个未调用的方法cellforrowatinexpath
这种行为的原因是我的numberOfRowsInSection返回了0行,而cellForRowAtIndexPath没有调用,因为它需要绘制0个单元格。我会对侯赛因·沙比尔的答案进行评论,以澄清这一点,但由于我还无法评论,我将发布一个答案 我也有同样的问题
numberOfRowsInSection
将启动,但cellForRowAt
不会启动。我撕开代码寻找原因,但原因不在代码中
(对我来说)解决方案就在故事板上。我没有为表视图设置约束选择表格视图,单击“添加新约束”图标(看起来像一个方形平手),并设置top、bottm、leading和Training的约束。然后将调用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
}()
什么是表视图框架?前面的答案解决了这个问题,但真正的解释是:[[1]:正确答案在这里您应该扩展您的答案--单行答案并没有真正的帮助。我正在使用UITableViewController。有相同的问题。我的解决方案是什么?是否设置了框架或设置了tableView的约束非常感谢!感谢您的“简单类的生命周期太短”,这是我的问题