【问题标题】:Objective C - cellForRowAtIndexPath Cell Never Nil?Objective C - cellForRowAtIndexPath 单元格从不为零?
【发布时间】:2015-09-12 19:47:07
【问题描述】:

我正在使用 QuickBlox 框架来构建一个聊天应用程序。下面是 cellForRowAtIndexPath 代码。

有些事情我只想对每个单元格执行一次(例如下载图像),所以据我所知,我应该添加 if (!cell) 块来执行此操作。

但是,即使是第一次加载 tableview,该块也不会真正触发。为什么会这样?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   QBChatMessage *message = [[ChatService shared] messagsForDialogId:self.dialog.ID][indexPath.row]; 
   ChatMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ChatMessageCellIdentifier];

   if (!cell) {
   // some things I only want to do once here, such as download images. but it never fires
   }

   [cell configureCellWithMessage:message];
   return cell;
}

【问题讨论】:

  • 如果您使用了registerClass:forCellReuseIdentifier:dequeueReusableCellWithIdentifier 不会返回 nil
  • 是的。你可以在你知道的文档中找到...
  • @rmaddy 我确实使用了 registerClass...还有其他方法可以让我做我想做的事吗?
  • 当然 - 删除 registerClass... 的使用。然后你就可以在cellForRow...做你想做的事了。

标签: ios objective-c uitableview tableviewcell


【解决方案1】:

如果您没有使用registerClass:forCellReuseIdentifier: 注册单元格,则对dequeueReusableCellWithIdentifier: 的调用只会返回nil

删除您对registerClass:forCellReuseIdentifier: 的使用,然后dequeueReusableCellWithIdentifier: 可以返回nil,您可以创建一个新单元格并在您的if 语句中正确初始化它:

if (!cell) {
    cell = [[ChatMessageTableViewCell alloc] init...]; // use proper init method
    // setup cell as needed for first time
}

【讨论】:

  • 谢谢,我一直都是通过注册课程来完成的。这很简单,帮助了我
【解决方案2】:

相反,您可以在 ChatMessageTableViewCell 类的初始化程序中执行一次性操作。这样您就可以保持您在注册时所习惯的行为。

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多