2011-03-08 110 views
12

我想从表视图中获取特定的单元格,以便我可以更改它的标签并停止活动指示器。cellForRowAtIndexPath返回零

我遇到的问题是cellForRowAtIndexPath返回零。

我的表格视图只有1行。

代码:

- (id) initWithNibName: (NSString*) nibNameOrNil bundle: (NSBundle*) nibBundleOrNil 
{ 
    self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; 

    if (self) 
    { 
     numberOfRows = 1; 
    } 

    return self; 
} 

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section 
{ 
    return numberOfRows; 
} 

-(void) stopCellIndicator 
{ 

    LiveUserFeedCell* cell = (LiveUserFeedCell*)[self.liveFeed cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
    [cell.activityIndicator stopAnimating]; 
    cell.middleLabel.text = @"N/a"; 
} 

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    static NSString *CellIdentifier = @"UserFeedCell"; 


    LiveUserFeedCell *cell = (LiveUserFeedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"LiveUserFeedCell" owner:self options:nil]; 
     cell = (LiveUserFeedCell *)[nib objectAtIndex:0]; 
    } 

    [cell.imgView setImage:[TUNinePatchCache imageOfSize:cell.imgView.frame.size forNinePatchNamed:@"bg_home_newsfeed_normal"]]; 


    if (shouldDisplayLoading == NO) 
    { 
     NSArray* songs = homeScreen.recentPlaybacks.songs; 
     TWSong* song = [songs objectAtIndex:index]; 
     cell.middleLabel.text = @""; 
     [cell.activityIndicator stopAnimating]; 

     UIFont *font = [UIFont fontWithName:@"Trebuchet MS" size:14];      

     NSString* kText =[NSString stringWithFormat:@"<b>%@</b> is listening to %@ by %@",song.user,song.title,song.artist]; 

     for(int i = 14; i > 0; i=i-1) 
     { 
      // Set the new font size. 
      font = [font fontWithSize:i]; 
      // You can log the size you're trying: NSLog(@"Trying size: %u", i); 

      /* This step is important: We make a constraint box 
      using only the fixed WIDTH of the UILabel. The height will 
      be checked later. */ 
      CGSize constraintSize = CGSizeMake(cell.isWatching.frame.size.width, MAXFLOAT); 

      // This step checks how tall the label would be with the desired font. 
      CGSize labelSize = [kText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

      /* Here is where you use the height requirement! 
      Set the value in the if statement to the height of your UILabel 
      If the label fits into your required height, it will break the loop 
      and use that font size. */ 
      if(labelSize.height <= cell.isWatching.frame.size.height) 
       break; 
     } 



     cell.isWatching.font = font; 
     cell.isWatching.text = [TTStyledText textFromXHTML:kText lineBreaks:YES URLs:YES]; 
     cell.isWatching.textAlignment = UITextAlignmentCenter; 

     ++index; 
     if (index == [songs count]) 
      index=0; 

    } 
    else 
    {  
     [cell.activityIndicator startAnimating]; 
    } 

    return cell; 
} 

但是,如果我这样做,我也得到了有效的细胞:

NSArray *indexPaths = [NSArray arrayWithObjects: 
         [NSIndexPath indexPathForRow:0 inSection:0], 
         nil]; 

numberOfRows = 0; 
[self.liveFeed deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; 


numberOfRows = 1; 
[self.liveFeed insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight]; 

LiveUserFeedCell* cell = (LiveUserFeedCell*)[self.liveFeed cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
[cell.activityIndicator stopAnimating]; 
cell.middleLabel.text = @"N/a"; 

任何想法如何解决这一问题? (可能是默认的第一个单元格是不是在行0 0段?)

+0

我看不到你在哪里覆盖cellForRowAtIndex。只要你不这样做,就不会有任何细胞。 – Krumelur 2011-03-08 13:15:20

+0

增加了更多的代码..现在看看。 thx – Idan 2011-03-08 13:19:53

回答

-4

显然的问题是,当我查询单元格时,tableView尚未配置。

+4

你是什么意思配置?你如何配置它? – Carlo 2012-07-10 08:15:43

+2

将两个手柄垂直向后移动。然后按下开始按钮,很简单。 – Jonny 2014-03-06 02:38:29

-6

尝试压倒一切:

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

没有。没有工作 – Idan 2011-03-08 22:17:16

0

使用try:

[self.livFeed beginUpdates]; 
LiveUserFeedCell* cell = (LiveUserFeedCell*)[self.liveFeed cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
[cell.activityIndicator stopAnimating]; 
cell.middleLabel.text = @"N/a"; 
[self.liveFeed endUpdates]; 

Apple UITableView doc

+0

如何在beginUpdates和endUpdates中获取cellForRowAtIndexPath来帮助确保单元不是零? – 2015-12-11 05:50:34

1

你已经解决了这个问题,虽然我已经遇到了类似的情况让我做了什么,我向您展示:

NSArray *cells = [contactsTable visibleCells]; 

    UITableViewCell *cell = nil; 
    NSIndexPath *path = [param objectAtIndex:1]; 
    for (UITableViewCell *aCell in cells) { 
     NSIndexPath *aPath = [contactsTable indexPathForCell:aCell]; 

     if ([aPath isEqual:path]) { 
      cell = aCell; 
     } 
    } 

奇怪,但试验&错误小时后工作好。

5

我遇到了同样的问题,cellForRowAtIndexPath返回零,并出于愚蠢的原因。在tableView:cellForRowAtIndexPath:中,我调用了一个调用方法[self.tableView cellForRowAtIndexPath:indexPath]。 (重新使用现有代码,并向下游移动一些逻辑。)

事实证明,该单元尚不可用于返回cellForRowAtIndexPath:。您可能需要在tableView:cellForRowAtIndexPath:之前做所有修改单元格的业务,或者我猜,通过*cell

13

我有cellForRowAtIndexPath返回零,我的原因是我在UITableView必须不完全初始化和填充的方法中调用它。一旦我将我的电话转到cellForRowAtIndexPath转换为-(void)viewDidAppear:(BOOL)animated,那么我的电池已正确返回。

+0

谢谢..什么是初学者的错误.. – Markus 2015-06-30 07:41:09

+1

好吧,我们都是在某个时候的初学者! :-) – 2015-12-09 15:29:12

26

值得一提的是,如果单元格不可见,cellForRowAtIndexPath:也会返回nil。

这可以穿线时尤其是一个问题......

+1

你如何处理这个问题?当另一个VC处于活动状态时,我需要对该单元格的引用。 – n00bProgrammer 2015-10-12 12:23:13

+0

“另一个VC”是什么意思?另一个VC(B)处于活动状态不会影响不同VC(A)中的细胞。这是指1个VC内的单元格,并且:如果由于它不存在而不活动,您将不会获得对单元格的引用。 VC回收一些细胞对象并重新使用它们。 – 2015-10-12 23:04:16

+0

保持单元格的副本并在需要的地方使用它有意义吗? – 2015-12-11 05:42:51

3

我得到了非常相似的条件和你在一起,但我通过调用“超级”的功能,而不是self.tableView解决了这个问题。我不知道为什么,但至少起作用。也许你可以试试。

[super tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
+0

顺便说一句,我的桌子是一个静态桌子,它可能与你的情况有所不同。 – Christopher 2015-02-22 10:34:50

+0

适合我,谢谢! – 2015-05-02 18:53:34

+0

根据苹果,从iOS 11,你必须防止使用tableView:cellForRowAtIndexPath:方法,否则它会崩溃你的应用程序。 – 2017-11-13 09:55:38