Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView仅在向下滚动时滞后_Ios_Uitableview_Lag - Fatal编程技术网

Ios UITableView仅在向下滚动时滞后

Ios UITableView仅在向下滚动时滞后,ios,uitableview,lag,Ios,Uitableview,Lag,我使用包含标签和2个图像的原型单元格创建了一个UITableView。每个单元格仅显示一个图像,该图像存储在本地且是静态的。加载时,用户位于tableview的底部。由于某些原因,UITableView在加载和向上滚动时都能完美执行。但是,向下滚动时,UITableView的延迟非常严重。到目前为止,我还不知道为什么会发生这种情况,特别是因为向上滚动的时间间隔为零 即使我从原型单元格中删除了一些元素,包括图像,也不会对性能产生明显影响。同时注释掉CellForRowatineXpath中的行(不

我使用包含标签和2个图像的原型单元格创建了一个UITableView。每个单元格仅显示一个图像,该图像存储在本地且是静态的。加载时,用户位于tableview的底部。由于某些原因,UITableView在加载和向上滚动时都能完美执行。但是,向下滚动时,UITableView的延迟非常严重。到目前为止,我还不知道为什么会发生这种情况,特别是因为向上滚动的时间间隔为零

即使我从原型单元格中删除了一些元素,包括图像,也不会对性能产生明显影响。同时注释掉CellForRowatineXpath中的行(不包括第一行和最后一行)也不会提高向下滚动的速度

#pragma mark UITableView Delegate
- (int)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessagingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
    if (!messages.count)
        return cell;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    ConvoParseData *convoParseData = [ConvoParseData new];
    NSString *sender = [convoParseData retreiveUsersNameFromArray:users withUserId:[[messages objectAtIndex:indexPath.row] valueForKey:@"sender"]];
    NSDate *date = [messages objectAtIndex:indexPath.row][@"date"];
    NSString *dateString = [convoParseData convoDateString:date];
    NSData *messageData = [[messages objectAtIndex:indexPath.row][@"emoji"] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *messageMeaning = [messages objectAtIndex:indexPath.row][@"meaning"];

    if (![[messages objectAtIndex:indexPath.row][@"meaning2"] isEqualToString:@"nil"]) {
        messageMeaning = [NSString stringWithFormat:@"%@\n%@", messageMeaning, [messages objectAtIndex:indexPath.row][@"meaning2"]];
    }
    if (![[messages objectAtIndex:indexPath.row][@"meaning3"] isEqualToString:@"nil"]) {
        messageMeaning = [NSString stringWithFormat:@"%@\n%@", messageMeaning, [messages objectAtIndex:indexPath.row][@"meaning3"]];
    }
    NSString *emoji = [[NSString alloc] initWithData:messageData encoding:NSNonLossyASCIIStringEncoding];
    int emojiCount = [emoji componentsSeparatedByString:@"   "].count;
    emoji = [emoji stringByReplacingOccurrencesOfString:@"   " withString:@""];
    cell.userMeaning.text = nil;
    [cell.userMeaning setHidden:YES];
    cell.friendMeaning.text = nil;
    [cell.friendMeaning setHidden:YES];

    //        If User
    if ([[messages objectAtIndex:indexPath.row][@"sender"] isEqualToString:[PFUser currentUser].objectId]) {
        cell.friendBubble.image = nil;
        cell.friendName.text = nil;
        cell.friendMessage.text = nil;
        cell.friendDate.text = nil;
        cell.userBubble.image = [UIImage imageNamed:@"chat_bubble_user.png"];
        cell.userDate.text = dateString;
        cell.userMessage.text = emoji;
        cell.userMessage.font = [UIFont fontWithName:nil size:28];
        if (emojiCount == 3) {
            cell.userMessage.font = [UIFont fontWithName:nil size:14];
        }
    } else {
        //            If Friend
        cell.userBubble.image = nil;
        cell.userMessage.text = nil;
        cell.userDate.text = nil;

        cell.friendName.text = sender;
        cell.friendDate.text = dateString;
        cell.friendMessage.text = emoji;
        cell.friendMessage.font = [UIFont fontWithName:nil size:28];
        if (emojiCount == 3) {
            cell.friendMessage.font = [UIFont fontWithName:nil size:14];
        }
    }

    return cell;
}


图:第一对驼峰来自应用程序启动。从那里到最后一个大驼峰之间的空间导航到UITableView。最后一个驼峰向下滚动(约60个单元格)分配从未超过20 mb。在最后一次驼峰时,时间剖面仪大部分停留在60%-80%的范围内,采样器经常击中40%-80%好吧,所以这不是一个简单的答案,但是Mike帮我找到的解决方案是从故事板原型单元转移到通过单元类以编程方式创建单元。迈克给了我这个解决方案,我要大喊一声

以下是新代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessagingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];

    if (!cell)
        cell = [[MessagingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageCell"];

    cell.message = [messages objectAtIndex:indexPath.row];
    return cell; 
}
MessagingTableViewCell.m

- (void)layoutSubviews {
    [super layoutSubviews];
    if (!_cellGenerated) {
        [self generateCell];
        _cellGenerated = YES;
    }

    [self resetCell];
}

- (void)generateCell {
//  Is only called the first time the cell is created
//  Static properties; properties that are the same for all cells, such as font size and color
}

- (void)resetCell {
//  Is called as the tableview reloads data
//  Any property that can be different for each cell, such as text
}

你试过运行探查器吗?我猜初始化自定义单元格需要很多时间。。。我知道你说你尝试删除元素,但没有看到原型,这是我的第一个猜测。发布你是如何实现MessagingTableViewCells的是个好主意。谢谢,我已经更新了问题以回应你们两个。向上滚动没有延迟,因为单元格已经存在,然而,当你向下滚动时,你正在排队/创建全新的单元格。如果不深入研究代码,这很可能就是为什么向下滚动而不是向上滚动时会出现性能下降的原因。另外两个快捷注释(通常是numberOfRowsInSection)应该返回NSInteger,而不是int,并且您可以简单地返回messages.count而不使用if语句,如果数组为空或为零,则解析为0。无论您执行什么操作,
generateCell
layoutSubviews
都是执行这些操作的错误位置。Opps!我猜你指的是我上面的.h错字。只是更正了。不,我指的是你的实现。不应生成(添加子视图?)
layoutSubviews
中的单元格。是否有其他方法?事实上,这似乎并不是完全正确的答案。这有助于提高iPhone模拟器的性能,但在我的物理版iPhone 5上,每次从上到下滚动时,tableview仍然滞后。我并不是说这是答案,因此这是评论,而不是答案。我只是想告诉您,您使用的
-layoutSubviews
是错误的。