cellForRowAtIndexPath返回nil

时间:2011-03-08 13:05:00

标签: iphone ios uitableview

我正在尝试从表格视图中获取特定单元格,以便我可以更改其标签并停止活动指示器。

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

我的表格视图只有一行。

代码:

- (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节?)

7 个答案:

答案 0 :(得分:33)

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

线程化时尤其如此......

答案 1 :(得分:15)

我让cellForRowAtIndexPath返回nil并且它的原因是我在UITableView必须没有完全初始化和填充的方法中调用它。一旦我将cellForRowAtIndexPath的电话移至-(void)viewDidAppear:(BOOL)animated,我的电池就会正确返回。

答案 2 :(得分:5)

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

事实证明,该单元格尚未可供cellForRowAtIndexPath:返回。您需要在tableView:cellForRowAtIndexPath:中修改单元格,或者我想通过*cell来完成所有业务。

答案 3 :(得分:3)

我和你的情况非常相似,但我通过调用&#34; super&#34;来解决这个问题。函数而不是self.tableView。我不知道为什么,但它至少起作用。也许你可以试试。

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

答案 4 :(得分: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 :(得分:0)

尝试使用:

[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

答案 6 :(得分:-5)

显然问题是当我查询单元格时,还没有配置tableView。