ios - 重新加载数据 VS。 reloadRowsAtIndexPaths

标签 ios objective-c iphone

我的问题如标题所示。在 iOS 7/iPhone 4 上这两种方法是有区别的。 问题是我正在以气泡聊天形式加载聊天对话。每个单元格都是一个包含文本消息、日期和消息状态(已发送、已接收、已查看或已收到)的气泡。

当我需要重新加载特定单元格时,我使用方法 reloadRowsAtIndexPaths 方法。它在 iOS 8(iPhone 4s 和 iPhone 5c)上完美运行。但是,它不适用于 iOS 7 (iPhone 4)。通过“工作”我的意思是它重新加载就可以了。但是在 iOS 7 的情况下,它只加载背景 View 而不加载文本或任何其他元素。

显然,这意味着重新加载时数据已找到(因为它在其他设备上确实有效)。我需要在 reloadRowsAtIndexPaths 之后使用 reloadData 来将整个内容加载到 iOS 7 (iPhone 4) 中。或者,我应该滚动表格 View 以加载单元格。

我确实解决了问题。但我想知道这是否是 iPhone 4 (iOS 7) 中的已知错误。

下面是一段可能有助于理解问题的代码:

- (void)tableView:(UIBubbleTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [self.bubbleTable cellForRowAtIndexPath:indexPath];
    if ([cell isKindOfClass:[UIBubbleTableViewCell class]] && isEditing) {
        NSBubbleData * bubble;
        // fetch the corresponding bubble index in the bubble data array
        int bubbleIndex = [self getArrayIndexForIndexPath:indexPath];
        bubble = [self.bubbleData objectAtIndex:bubbleIndex];
        // making sure it's either a msg sent by me or msg sent by someone else. In other words, making sure it's neither a system message nor is it a header date or something.
        if (bubble.type != BubbleTypeMine && bubble.type != BubbleTypeSomeoneElse)
            return;

        // configure the bubble to be [de]selected
        bubble.isSelected = !bubble.isSelected;

        // add the Key-Value pair to the dictionary
        if ([selectedIndices valueForKey:bubble.msgID]) [selectedIndices removeObjectForKey:bubble.msgID];
        else [selectedIndices setValue:[NSNumber numberWithInt:bubbleIndex] forKey:bubble.msgID];

        // update gui
        [self.bubbleTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        if([[self machineName] isEqualToString:@"iPhone3,1"] ||
           [[self machineName] isEqualToString:@"iPhone3,2"] ||
           [[self machineName] isEqualToString:@"iPhone3,3"]) { // iphone 4
            [self.bubbleTable reloadData];
        }
#if DEBUG
        printf("Selected Message Text: %s\n", [[[_anasDb getFromChatRoom:self.contactInternationalPhone message:bubble.msgID] valueForKey:MESSAGE_TEXT] UTF8String]);
#endif
    }
}

很明显,您不必了解所有内容。以下是代码的内容(在问题的上下文中):

  1. 获取单元格的实例。
  2. 编辑它的一些属性,使其在下次重新加载后表现不同
  3. 使用 reloadRowsAtIndexPaths 重新加载该特定单元格 - 到目前为止,它适用于所有设备,除了需要 reloadData 的 iPhone 4 (iOS 7)。

这是 cellForRowAtIndexPath 的相关部分:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIBubbleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    NSBubbleData *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row - 1];

    if (cell == nil) cell = [[UIBubbleTableViewCell alloc] init];

    cell.data = data;
    cell.showAvatar = self.showAvatars;
    if (data.isSelected) {
        cell.dim = YES;
        cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.39 blue:0.106 alpha:0.2f];
    } else {
        cell.backgroundColor = [UIColor clearColor];
    }
    return cell;

提前致谢。

最佳答案

您的代码存在问题,您需要将数据加载到内部单元格中

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

- (void)tableView:(UIBubbleTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中调整您的数据,然后为指定的行调用 reloadRowsAtIndexPaths。不要在该方法内更改单元格标签(或者如果您不想重新加载特定行,您可以在此处更改数据和单元格内容)

重新加载数据会重新加载所有行,这在大多数情况下不是您需要的,因为它会停止滚动并重新计算高度、部分计数等

关于ios - 重新加载数据 VS。 reloadRowsAtIndexPaths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744074/

相关文章:

ios - 如何生成多维核心数据对象的列表

ios - 设置 PreferredContentSize 在 iOS8 横向 iPhone 的今日小部件中不起作用

objective-c - Objective-C 属性的解释

ios - self.window cornerRadius - 没有状态栏

iphone - Core Text - 从 NSRange 获取像素坐标

ios - 在 iOS 上调用获取 CFStream 的 native 套接字总是失败

ios - 避免向 Realm 添加重复对象

ios - 如何根据 X/Y 坐标在屏幕上选择 UIView

android - 适用于 Android/iPhone/黑莓设备的聊天服务器

ios - 如何让 uitableview 知道在哪里滚动未创建的 View