uitableview cellforrowatindexpath not called but n

2020-07-18 10:52发布

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

8条回答
趁早两清
2楼-- · 2020-07-18 11:26

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
}
查看更多
小情绪 Triste *
3楼-- · 2020-07-18 11:30

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;
查看更多
smile是对你的礼貌
4楼-- · 2020-07-18 11:30

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.

查看更多
我只想做你的唯一
5楼-- · 2020-07-18 11:41

You can refer following code to reload your tableView

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
查看更多
手持菜刀,她持情操
6楼-- · 2020-07-18 11:45

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

查看更多
smile是对你的礼貌
7楼-- · 2020-07-18 11:45

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);
}
查看更多
登录 后发表回答