uitableview cellforrowatindexpath not called but n

2020-07-18 11:43发布

问题:

in my tableview no of rows in section method is called and it returns value 17,but the cellforrowatindexpath is not getting called.i have put breakpoints in the first line of this method but this point is never shown when debugging,i have followed the tableviewdelegate and datasource.and the tableviews datasource,delegate are properly set in the Int builder.

i am also posting some of the code

in my viewcontroller.m

-(void)viewDidLoad
{

        tweets=[[NSMutableArray alloc]init];
        [self updateStream:nil];
        [self.tweetsTable setDelegate:self];
        [self.tweetsTable setDataSource:self];

}


-(NSInteger)tableView:(UITableView *)tweetsTable numberOfRowsInSection:(NSInteger)section {

    return [tweets count];

}

-(UITableViewCell *)tableView:(UITableView *)tweetsTable cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"twitCell";
    TwitCell *cell = (TwitCell *)[tweetsTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = (TwitCell *)[[[TwitCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   
    }
    cell.tweet = [tweets objectAtIndex:indexPath.row];
    [cell layoutSubviews];
    return cell;
}

回答1:

cellForRowAtIndexPath won't get called if your tableview has height of 0

make sure your tableview always has VISIBLE rows

CGRect frame = self.tableView.frame;
frame.size.height = 100;
self.table.frame = frame;


回答2:

It will not get called if you are returning 0 rows in numberOfRowsInSection method. Please check the number of rows that you return.



回答3:

Given the code you've shown us there only a few possibilities. Either self.tweetsTable is nil, tweets is nil, or tweets contains no element and count is returning zero. Now I know you say that everything is correct, but clearly something is up! You can add a bit of defensive code to detect these problems.

-(void)viewDidLoad
{

        tweets=[[NSMutableArray alloc]init];
        [self updateStream:nil];
        NSAssert(self.tweetsTable, @"self.tweetsTable must not be nil.");
        [self.tweetsTable setDelegate:self];
        [self.tweetsTable setDataSource:self];

}


-(NSInteger)tableView:(UITableView *)tweetsTable numberOfRowsInSection:(NSInteger)section {

    NSAssert(tweets, @"tweets must not be nil here");
    NSUInteger n = [tweets count];
    if(n == 0)
        NSLog(@"WARNING: %@ returning 0", __PRETTY_FUNCTION__);
    return (NSInteger)n;

}

If you do this and one of the asserts fires you'll know where your problem is. If no assert fires then something is going on outside the scope of the code you have shown (e.g. something is getter released to soon or memory getting clobbered). Oh and one final thing -- can you see the empty table view on the screen? If you table is not visible AFAIK cellForRowAtIndexPath won't be called.



回答4:

check this

or else use viewDidLoad code in viewWillAppear



回答5:

Here is my function. After I placed "dispatch_async(dispatch_get_main_queue()..." inside of "tweets = [NSJSONSerialization.....", it works.

tweets = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];
if (tweets) {
    // We have an object that we can parse
    NSLog(@"%@", tweets);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
} 
else { 
    // Inspect the contents of jsonError
    NSLog(@"%@", jsonError);
}


回答6:

In my cases I had created a UITableView as a property on my view controller, but I forgot to add it as a subview to self.view

Strangely you will get the symptoms that sujith described: numberOfRowsInSection will be called, but cellForRowAtIndexPath will not!

This was my missing line:

[self.view addSubView:self.myTableViewProperty];

And to defend against it:

NSAssert(self.myTableViewProperty.superview != nil, @"The table view dose not have a superview");
[self.myTableViewProperty reloadData];


回答7:

You can refer following code to reload your tableView

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];


回答8:

In my case I was using custom subclass of UITableView and I have not called super super.layoutSubviews()

override func layoutSubviews() {
    super.layoutSubviews() // Forgotten to call super implementation
    self.layer.borderColor = Constants.primaryColor.cgColor
    self.layer.borderWidth = Constants.primaryBorderWidth
    self.indicatorStyle = .white
}