2011-05-18 113 views
3

在我的tableview中调用方法中的行no的方法被调用,它返回值17,但cellforrowatindexpath没有被调用。我已经把断点放在第一行这个方法,但这一点从来没有显示调试时,我已经遵循tableviewdelegate和datasource.and tableviews数据源,委托在Int构建器中正确设置。uitableview cellforrowatindexpath没有调用,但没有:行中的行被称为

我也张贴一些代码

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

回答

2

,如果你正在重返numberOfRowsInSection方法0行它不会被调用。 请检查您返回的行数。

+0

其返回17。我发现,调试 – sujith1406 2011-05-18 11:12:08

0

检查this

或者使用的viewDidLoad代码在viewWillAppear中

2

鉴于你已经证明我们有只有几种可能性的代码。 self.tweetsTablenil,tweetsniltweets不包含元素并且计数返回零。现在我知道你说一切都是正确的,但显然有些事情已经结束了!您可以添加一些防御性代码来检测这些问题。

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

} 

如果你这样做,并且其中一个断言触发,你就会知道你的问题在哪里。如果没有声明触发,那么你所展示的代码范围之外的事情正在发生(例如,getter被释放到很快或内存被破坏)。哦,还有最后一件事 - 你能看到屏幕上的空表视图吗?如果您的表不可见,AFAIK cellForRowAtIndexPath将不会被调用。

+0

当雅我的tableview是不是visible.what这说明什么? – sujith1406 2011-05-18 11:15:46

0

这是我的功能。在将“dispatch_async(dispatch_get_main_queue()...”放入“tweets = [NSJSONSerialization.....”之后,它可以正常工作。

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

在我的情况下,我创造了一个UITableView我的视图控制器上的属性,但我忘了把它作为一个子视图添加到self.view

奇怪的是,你会得到sujith描述的症状:numberOfRowsInSection会被称为,但cellForRowAtIndexPath不会!

这是我的思念线:

[self.view addSubView:self.myTableViewProperty]; 

,并捍卫反对:

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

cellForRowAtIndexPath不会被调用,如果你的tableview有0

高度确保您的tableview总是有可见行

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

在我的情况下,这是问题,你的问题解决了它!感谢Zayin Krige – 2016-11-13 11:17:41

0

你可以参考下面的代码来重新加载你的tableView

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

在我来说,我使用的UITableView的自定义子类,我还没有所谓的超级超级。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 
} 
相关问题