繁体   English   中英

UITableView tableView:cellForRowAtIndexPath:没有被调用

[英]UITableView tableView:cellForRowAtIndexPath: not getting called

我在一个视图中有3个表视图,我想知道为什么未调用tableView:cellForRowAtIndexPath:

 #pragma mark -
 #pragma mark <UITableViewDelegate>
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if (tableView == self.mainVoucherTableViewController)
       [self setSelectedIndex:indexPath.row];
 }

 - (NSInteger)tableView:(UITableView *)tableView
   numberOfRowsInSection:(NSInteger)section
 {
    if (tableView == self.mainVoucherTableViewController){
  return 10;
     }
 }

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if (tableView == self.mainVoucherTableViewController){
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    }

    cell.textLabel.text = @"THISTEXT";
    return cell;
   }
}


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 if (tableView == self.mainVoucherTableViewController)
     return 1;
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
if (tableView == self.mainVoucherTableViewController){
    NSString * myString = [NSString stringWithFormat:@"HELLLO WORLD"];
    return myString;
  }
}

有人会知道为什么吗? 基本上,这不会创建任何单元格或显示单元格。 它仅显示表视图。 :(

首先在tableView:cellForRowAtIndexPath:方法内部登录,以查看是否在if语句之外以及内部都调用了该方法,以帮助缩小问题范围。

另外,请尝试不要比较您的:

tableView == self.mainVoucherTableViewController

将tableViews设置为具有标签值。 然后,您可以执行以下操作:

if(tableView.tag == 100){ // tag number we assigned self.mainVoucherTableViewController via IB
    //do your stuff here
}

只是为了整合以上几点:

  1. 从您的命名来看,您的tableView称为mainVoucherTableViewController-仅想确认这确实是UITableView而不是UITableViewController? 如果它是一个UITableViewController,那么由于明显的原因(或者不是那么明显-告诉我并可以进一步解释),其余的这些将无法正常工作

  2. 确保使用以下代码或在Interface Builder中将当前viewController设置为tableView的委托和dataSource(如果使用的是XIB)

     self.mainVoucherTableViewController.delegate = self; self.mainVoucherTableViewController.dataSource = self; 
  3. 确保您的numberOfRowsInSection函数正在被调用,并且您返回的非零值(放入NSLogs等),并且对numberOfSections也执行相同的操作(实际上,如果仅使用1个部分,则不需要numberOfSections)-请参阅UITableViewDataSource协议参考: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

  4. 根据先前的帖子,在开始时以及返回之前记录您的cellForRow(如果检查了第1-3点并正常工作)以确保它已被触发。 还要对要返回的单元格执行NSLog,以确保其不为零。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     if (tableView == self.mainVoucherTableViewController)
    {
       return 10;
     }
      else
     {retun 5;
     }
   }

它在第一张表中显示10行,第二张表中显示5行。

实例声明的顺序确实很重要。 例如,如果您有一个名为tableView的ivar:

错误

self.tableView.delegate = self;
self.tableView = [UITableView alloc] init];

正确

self.tableView = [UITableView alloc] init];
self.tableView.delegate = self;

检查UITableView对象框架大小。 可能帧大小不足以绘制Cell。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM