reloadData VS. reloadRowsAtIndexPaths

时间:2015-02-26 13:42:52

标签: ios objective-c iphone

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

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

显然,这意味着重新加载时的数据是找到的(因为它确实可以在其他设备上运行)。我需要在reloadData之后使用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
    }
}
显然,你不必得到它的每一点。这是代码所说的内容(在问题的上下文中):

  1. 获取单元格的实例。
  2. 编辑其中的一些属性,以便下次重新加载后行为会有所不同
  3. 使用reloadRowsAtIndexPaths重新加载该特定单元格 - 除了需要reloadData的iPhone 4(iOS 7)之外,它适用于所有设备。
  4. 这是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;
    

    提前致谢。

2 个答案:

答案 0 :(得分:2)

您的代码问题是您需要将数据加载到

中的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

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

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

答案 1 :(得分:0)

您是否尝试过使用beginUpdates和endUpdates?

[self.bubbleTable beginUpdates];
[self.bubbleTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[self.bubbleTable endUpdates];
相关问题