UITableView仅向下滚动

时间:2014-09-17 15:10:03

标签: ios uitableview lag

我使用包含标签和2张图片的原型单元格创建了一个UITableView。每个单元格仅显示1个图像,并且图像存储在本地并且是静态的。加载后,用户位于tableview的底部。出于某种原因,UITableView在加载和向上滚动时表现都很完美。但是,向下滚动时,UITableView严重滞后。到目前为止,我对此失败了,特别是因为向上滚动没有滞后。

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

#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%

1 个答案:

答案 0 :(得分:0)

好的,所以对此没有一个简单的答案,但迈克帮助我解决的解决方案涉及从故事板原型单元转移,而是通过单元类以编程方式创建单元格。迈克大声喊出来给我这个解决方案!

这是新代码:

- (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
}