【发布时间】:2017-03-14 20:13:26
【问题描述】:
我知道这个问题已经被问过很多次了,但不幸的是,这些问题中的解决方案都不适合我。
以下是我尝试过的一些事情:
确保numberOfSectionsInTableView 没有返回0
确保numberOfRowsInSection 没有返回0。
确保使用performSelectorOnMainThread 在主线程上调用 reloadData,如下所示:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
设置委托:
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 中的哪里设置委托和数据源?
-
Anbu,我已经以编程方式创建了 tableview。
-
Priyal,是的,我已经实现了相同的 tableview。
-
可以添加相关代码吗?
标签: ios objective-c uitableview