gpt4 book ai didi

iphone - cellForRowAtIndexPath 返回 nil

转载 作者:技术小花猫 更新时间:2023-10-29 10:11:48 26 4
gpt4 key购买 nike

我正在尝试从 TableView 中获取特定的单元格,以便我可以更改它的标签并停止事件指示器。

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

我的 TableView 只有 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 节中?)

最佳答案

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

这在线程化时尤其是一个问题...

关于iphone - cellForRowAtIndexPath 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5232849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com