我知道这个问题已经被问了很多次,但不幸的是,这些问题中的解决方案都不适合我。
以下是我尝试过的一些事情:
确保 numberOfSectionsInTableView
不返回 0
确保 numberOfRowsInSection
不返回 0
。
使用 performSelectorOnMainThread
确保在主线程上调用 reloadData,如下所示:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
设置委托(delegate):
self.tableView.dataSource = self; self.tableView.delegate = self;
确保 tableview 背景颜色已更改。
确保 tableview 框架是可调整的。
但是 cellForRowAtIndexPath
方法从未调用过
请找到代码:
UITableView *tableView;
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = "test";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
如果您有任何想法,请告诉我。
谢谢
最佳答案
您在 ViewDidLoad() 中创建了 tableView,将其作为 subview 添加到您的 ViewController View 中,这很好 :) 但是您在哪里调用 tableView 上的重新加载数据 buddy :)
添加
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]
[self.tableView reloadData];
}
它会像一个魅力:)
关于ios - 目标 - C - UITableView cellForRowAtIndexPath 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782116/