reloadData VS. reloadRowsAtIndexPaths

jok*_*ker 5 iphone objective-c ios

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

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

显然,这意味着重新加载时的数据是找到的(因为它确实在其他设备上工作).我需要使用reloadDataafter reloadRowsAtIndexPaths来在iOS 7(iPhone 4)中加载整个东西.或者,我应滚动表视图以加载单元格.

我确实解决了这个问题.但我想知道这是否是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
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,你不必完全了解它.这是代码所说的内容(在问题的上下文中):

  1. 获取单元格的实例.
  2. 编辑它的一些属性,以便下次重新加载后它的行为会有所不同
  3. 重新加载使用的特定单元格reloadRowsAtIndexPaths- 除了需要的iPhone 4(iOS 7)之外,它可以在每个设备上运行reloadData.

以下是相关部分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;
Run Code Online (Sandbox Code Playgroud)

提前致谢.

小智 2

您的代码的问题是您需要将数据加载到单元格内

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

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

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