ios - 目标 - C - UITableView cellForRowAtIndexPath 从未调用过

标签 ios objective-c uitableview

我知道这个问题已经被问了很多次,但不幸的是,这些问题中的解决方案都不适合我。

以下是我尝试过的一些事情:

确保 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/

相关文章:

ios 8 交互式通知不显示操作

ios - Firebase 突然崩溃并显示 "RepoExists"消息

iphone - ".../libtool: can' t 找到 : -lxml2"的文件

ios - NSRangeException',原因 : '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array'

ios - 对于 iOS 项目,我应该将什么 XML 解析器与 Delphi XE2 一起使用?

ios - 使用点击手势识别器禁用提示 View

iphone - iOS通用开发——Xib文件和图片名称中波浪符号(~)的使用区分

ios - 如何根据按下的按钮更改 UITableView 中 Label 的文本?

ios - Xcode 6 : Label in Table Cell won't expand/display longer text upon expansion/line up with cell

ios - 将 UIButton 的触摸限制在指定区域