简体   繁体   中英

cellForRowAtIndexPath is not getting called

I have a table view controller in which the cells don't show up: I have added UiViewDataSource and UITableViewDelegate as shown below:

myViewController:UITableViewController<UITableViewDataSource,UITableViewDelegate>

In the storyboard as well as in the code I have set

self.tableView.delegate =self;
self.tableview.datasource = self;

My numberOfRowsInSection returns the value 30 (verified with logs) and numberOfSectionsInTableView returns 1 (verified with logs)

I have also printed the tableView header and footer view frame,height and width and they all have the value 0.0000

NSLog(@"The header frame size is%f" ,self.tableView.tableHeaderView.frame.size.height);
NSLog(@"The header frame size is%f" ,self.tableView.tableHeaderView.frame.size.width);
NSLog(@"The header frame size is%f" ,self.tableView.tableFooterView.frame.size.height);
NSLog(@"The header frame size is%f" ,self.tableView.tableFooterView.frame.size.width);

This is the firstController of a navigation controller. The numberOfSectionsInTableView method is called first, then the numberOfRowsInSection and then the heightForRowAtIndexPath is called.

The cellForRowAtIndexPath method contains:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = (CustomCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell configureCustomCell:[self.dataSource objectAtIndex:indexPath.row]];

    return cell;
}

configureCustomCell configures the different properties in the cell.

I tried all the solutions given in similar questions but none of them seem to be working. What am I missing?

if you have configured UITableViewDataSource and UITableViewDelegate protocols inside your ViewController.h file, then there is no need to use self.tableView.delegate =self;

self.tableview.datasource = self;

if you have configured UITableView inside your storyboard. you need to configure your CustomTableViewCell with your tableView's cell property.

在此处输入图片说明 step - 1 select your tableView Cell and inside identity inspector define your CustomTableViewCell class.

在此处输入图片说明

step - 2 now provide cell identifier. (i have used "customCell")

在此处输入图片说明

import your CustomCell class inside your ViewController.m file. Just created demo app as per your reference. Hope it solves your query.

#import "ViewController.h"
#import "CustomTableViewCell.h" //CustomTableViewCell class

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad 
{
      [super viewDidLoad];
       // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 30; //used staticCells 30
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"customCell"; //as defined in cell identifier (as per step -2)

    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil)
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    // configure your custom cell 

    return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

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