简体   繁体   中英

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.

 #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.

Also try instead of comparing your:

tableView == self.mainVoucherTableViewController

Set the tableViews to have tag values instead. 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? 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)

  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

     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

  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. Also do an NSLog of the cell you're returning just to make sure it isn't nil.

- (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.

The order of instance declaration does matter. For example, if you have a ivar called tableView :

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. maybe Frame size is not enough to draw Cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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