cellForRowAtIndexPath 返回 nil

2024-01-16

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

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

我的表视图只有 1 行。

Code :

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

当线程化时,这尤其可能成为一个问题......

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cellForRowAtIndexPath 返回 nil 的相关文章

随机推荐

  • 终端上的“快速构建”抛出“错误:找不到根清单”

    我想在终端上运行我的快速编程 所以我cd我的项目的根文件夹 然后运行 swift build 但是出现了错误 error root manifest not found有什么帮助吗 如果其他人偶然发现同样的问题 我的解决方案是 cd 到我的
  • 在 Windows 上运行 Django 时出现“WinError 10013”

    自从我遇到这个问题以来已经快一个月了 我非常感谢您的帮助 尝试登录我的 Django Web 应用程序时 我在 accounts login 处遇到 OSError 我能够登录 127 0 0 1 8000 admin 但不能登录 acco
  • findDOMNode 与 getElementById 对于普通 DOM 元素

    我不太确定这个问题有真正的答案 但我想知道是否最好使用以下命令在 React 应用程序中查找常规 DOM 元素 A refs 和 ReactDOM findDOMNode or b 普通旧 document getElementById 我
  • 导入而不执行类 - python

    我的问题是我有一个包含类的文件 并且在这个类内有一堆代码将被执行 所以每当我导入该文件时它就会执行 无需创建该类的对象 这是例子 FILE X class d def init self print print this will NOT
  • Gradle 和 Android 支持库

    几乎在每个 Android 应用程序中 我们都需要一些库项目 例如 ABS HoloEverywhere 等 其中大部分都在 Maven Central 中 这很好 不好的是 它们中的大多数都依赖于支持库 并且自然地指向 Maven Cen
  • 在双变量中获取数字的问题

    我的 java 程序中需要的函数出现了一些问题 我想检查 双精度 变量的总位数 例如 5 应该返回 1 5 0034 应该返回 5 2 04 应该返回 3 我的函数是这样的 private int getDoubleLength doubl
  • WPF Datagrid 绑定自定义列标题

    我试图弄清楚如何使用 MVVM 模式将 WPF DataGrid 的列标题和主要数据绑定到数据源 我正在寻找的结果如下所示 source vallelunga com http brian vallelunga com files data
  • 浮点按位运算的用处

    我注意到浮点存在 SSE 指令 这让我想知道 您可以对 fp integer union 中的标量执行相同的操作 我突然想到 如果对浮点数数组的各个分量进行按位或运算 则可以通过查看结果的符号位来快速确定它们中是否有任何一个为负数 浮点值的
  • 获取 TWIG 模板中的控制器名称

    我正在学习 symfony2 3 当我尝试在 twig 模板中获取控制器名称时出现错误 控制器 namespace Acme AdminBundle Controller use Symfony Bundle FrameworkBundle
  • Mac OS-X Mountain Lion 上的 GCC-4.2 错误,无法使用 pip / virtualenv 安装某些软件包

    我看到一个非常烦人的错误 我真的不知道如何处理 这似乎很常见 我几乎尝试了所有能找到的解决方案 但都无济于事 我正在尝试使用 pip 安装库 gevent psycopg2 和 greenlet 都遇到过这个问题 问题似乎是我的计算机找不到
  • 如何从服务器在 ViewPager 内的 VideoView 上播放视频

    我尝试开发一个应用程序从服务器检索视频并在 viewpager 内的 videoview 上播放 原始文件夹中的视频工作正常 但有两个问题 1 部分视频无法播放 或黑色活动显示 2 页面滚动时视频不停止 那么如何使用 URL 而不是 and
  • 如何在打开另一个 Javascript 下拉菜单时关闭另一个下拉菜单

    我不太熟悉 JavaScript 我希望能得到一些帮助来解决我似乎无法解决的问题 目前我的网站上有 2 个下拉菜单 一种是用于导航的下拉菜单 单击汉堡菜单图标时会激活该菜单 第二个下拉列表用于显示我网站上的类别 目前 当我单击一个下拉列表时
  • 检测浏览器缓存是否已满

    我们发现浏览器缓存已满是导致酒店后台出现问题的原因 它只影响我们的一小部分用户 但我们希望提醒他们该问题 并为他们提供一些如何自行解决问题的指导 我们希望使用与 GMail 使用的系统类似的系统 当它检测到您的浏览器缓存已满表现不正常时 它
  • Kafka中的延迟消息消费

    如何使用 Apache Kafka 生成 消费延迟消息 似乎标准 Kafka 和 Java kafka client 功能没有此功能 我知道我可以用标准的等待 通知机制自己实现它 但它看起来不太可靠 因此任何建议和良好实践都值得赞赏 Fou
  • Android Shake(Sensor) 服务,用于应用程序后台的震动检测

    我开发了一个具有晃动功能的应用程序 以便某些功能正常工作 因此我使用晃动类并实现了主活动 以便在应用程序运行时顺利工作 但我的问题是当我的应用程序进入睡眠或终止或停止时 如何检测背景震动 服务 我的摇摇班 public class Shak
  • 区别 - “e.preventDefault();”和“返回假;” [复制]

    这个问题在这里已经有答案了 有什么区别return false and e preventDefault e preventDefault 将防止默认事件发生并且return false停止传播
  • Android - Firebase 用户身份验证令牌会过期吗?

    我决定使用Volley并走 RESTful 路线Firebase因为当没有互联网连接时 他们的听众似乎会挂起 至少对于 Volley 它可以让我知道网络请求是否由于互联网连接而失败 我需要知道是否FirebaseUser身份验证令牌是否过期
  • 在基础对象中构造派生类型的对象

    vb net 中是否可以有一个方法来构造 bass 类中任何派生类的对象 在此代码中 x Clone 应返回一个 Bar 对象 这是使用两种不同对象类型在两个类中重复代码的唯一方法 Module Module1 Sub Main Dim x
  • .NET:我可以使用 DataContractJsonSerializer 序列化为 JSON 关联数组吗?

    使用时数据契约Json序列化器 http msdn microsoft com en us library system runtime serialization json datacontractjsonserializer aspx序
  • cellForRowAtIndexPath 返回 nil

    我正在尝试从表格视图中获取特定的单元格 以便我可以更改它的标签并停止活动指示器 我遇到的问题是cellForRowAtIndexPath返回零 我的表视图只有 1 行 Code id initWithNibName NSString nib