简体   繁体   English

UITableView tableView:cellForRowAtIndexPath:没有被调用

[英]UITableView tableView:cellForRowAtIndexPath: not getting called

I have 3 table views in one view and I was wondering why the tableView:cellForRowAtIndexPath: was 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;
  }
}

WOULD anyone know why this is? 有人会知道为什么吗? Basically this doesn't create any cell or display cells. 基本上,这不会创建任何单元格或显示单元格。 It just displays the table views. 它仅显示表视图。 :( :(

Start off by logging inside your tableView:cellForRowAtIndexPath: method to see if it gets called at all outside your if statement as well as inside to help narrow down the issue. 首先在tableView:cellForRowAtIndexPath:方法内部登录,以查看是否在if语句之外以及内部都调用了该方法,以帮助缩小问题范围。

Also try instead of comparing your: 另外,请尝试不要比较您的:

tableView == self.mainVoucherTableViewController

Set the tableViews to have tag values instead. 将tableViews设置为具有标签值。 Then you can do: 然后,您可以执行以下操作:

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

Just to consolidate a few things from above: 只是为了整合以上几点:

  1. From your naming, your tableView is called mainVoucherTableViewController - just want to confirm that this is indeed a UITableView and not a UITableViewController? 从您的命名来看,您的tableView称为mainVoucherTableViewController-仅想确认这确实是UITableView而不是UITableViewController? If it's a UITableViewController then the rest of this won't work for obvious reasons (or not so obvious - let me know and can explain further) 如果它是一个UITableViewController,那么由于明显的原因(或者不是那么明显-告诉我并可以进一步解释),其余的这些将无法正常工作

  2. Make sure you have set the current viewController as the tableView's delegate and dataSource, either with the code below or in Interface Builder if you're using a XIB 确保使用以下代码或在Interface Builder中将当前viewController设置为tableView的委托和dataSource(如果使用的是XIB)

     self.mainVoucherTableViewController.delegate = self; self.mainVoucherTableViewController.dataSource = self; 
  3. Make sure your numberOfRowsInSection function is being called and that you're returning non-zero (put in NSLogs, etc) and do the same for numberOfSections as well (actually numberOfSections isn't required if you're only using 1 section) - see UITableViewDataSource Protocol Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html 确保您的numberOfRowsInSection函数正在被调用,并且您返回的非零值(放入NSLogs等),并且对numberOfSections也执行相同的操作(实际上,如果仅使用1个部分,则不需要numberOfSections)-请参阅UITableViewDataSource协议参考: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

  4. As per previous post, log your cellForRow (if points #1-3 are checked and working) at the beginning to make sure it's triggered, and just before the return. 根据先前的帖子,在开始时以及返回之前记录您的cellForRow(如果检查了第1-3点并正常工作)以确保它已被触发。 Also do an NSLog of the cell you're returning just to make sure it isn't nil. 还要对要返回的单元格执行NSLog,以确保其不为零。

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

It display row in first table 10, second table show 5 rows. 它在第一张表中显示10行,第二张表中显示5行。

The order of instance declaration does matter. 实例声明的顺序确实很重要。 For example, if you have a ivar called tableView : 例如,如果您有一个名为tableView的ivar:

WRONG 错误

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

CORRECT 正确

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

check UITableView Object Frame Size. 检查UITableView对象框架大小。 maybe Frame size is not enough to draw Cell. 可能帧大小不足以绘制Cell。

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

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