【问题标题】:Objective - C - UITableView cellForRowAtIndexPath never calledObjective - C - UITableView cellForRowAtIndexPath 从未调用过
【发布时间】: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


【解决方案1】:

您创建的 tableView 将其作为 subView 添加到 ViewDidLoad() 中的 ViewController 的视图中,这很好 :) 但是您在哪里调用 tableView 好友上的 reload Data :)

添加

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

它会像魅力一样发挥作用:)

【讨论】:

  • 为什么需要 reloadData?
【解决方案2】:

只需从模拟器中卸载应用程序并再次运行。有时它不起作用。

【讨论】:

  • 近 10 次做了同样的事情,并且干净地构建了模拟器和设备。但它不会工作。
  • 您的 viewdidload 代码放入 ViewDIDAppeer。可能是 self 不分配给委托和数据源。
【解决方案3】:

你必须委托和数据源协议来查看控制器

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

【讨论】:

    【解决方案4】:

    第一

    @interface yourController ()<UITableViewDelegate,UITableViewDataSource>
    

    第二

    -(void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
    
    [self reloadData]; //call your webservice here
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView]
    [self.tableView reloadData]; //most imp
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return yourArray.count; //your array which is get from webservice
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      相关资源
      最近更新 更多