简体   繁体   中英

uitableview cellforrowatindexpath not called but no:of rows in section is called

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;
}

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;

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

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

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);
}

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];

您可以参考以下代码重新加载tableView

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

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
}

check this

or else use viewDidLoad code in viewWillAppear

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