Ios UITableview滚动速度慢,或单元格位置不匹配

Ios UITableview滚动速度慢,或单元格位置不匹配,ios,objective-c,uitableview,Ios,Objective C,Uitableview,当滚动滞后时,MyUITableView。这是我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CellListsFriend *cell = [tableView dequeueReusableCellWithIdentifier:@"cellListsFriend" forIndexPath:indexPath];

当滚动滞后时,My
UITableView
。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellListsFriend *cell = [tableView dequeueReusableCellWithIdentifier:@"cellListsFriend" forIndexPath:indexPath];
    if (!cell.selected) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    ContactAlphaB *contact = [self objectAtIndexPath:indexPath];
    NSMutableAttributedString *strFullname;
    if (strFullname == nil) {
        strFullname = [NSString DrawColorForFullname_firstName:contact.firstName lastName:contact.lastName];
    }
    cell.lblNameFriend.attributedText = strFullname;
    cell.lblStatusSentense.text = contact.usernameIsUsing;
    [cell.lblStatusSentense setTextColor:[SomeMethods_TrinhVM TrinhVM_colorWithHexString:color_status_ShowUser]];
    if ([contact.registerType isEqualToString:@"1"]) {
        cell.lblRegisterType.text = @"Email";
    }
    else{
        cell.lblRegisterType.text = @"Mobile";
    }
    [NSURLConnection getImage:contact.linkAvatar showImage:cell.imgAvatarFriends];
    [cell.lblRegisterType setTextColor:[SomeMethods_TrinhVM TrinhVM_colorWithHexString:@"777777"]];
    // Rounded Rect for cell image
    cell.imgAvatarFriends.layer.borderWidth=2;
    cell.imgAvatarFriends.layer.borderColor=[[UIColor whiteColor]CGColor];
    [cell.imgAvatarFriends.layer setCornerRadius:cell.imgAvatarFriends.frame.size.width/2];
    [cell.imgAvatarFriends.layer setMasksToBounds:YES];
    return cell;
}
我的方法从文档方向获取图像:

+(UIImageView *)getImage:(NSString*)strImageName showImage:(UIImageView*)showImage{
    NSString* fileName = [NSString stringWithFormat:@"%@",strImageName];
    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString *path = [arrayPaths objectAtIndex:0];
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
    NSData *data = [NSData dataWithContentsOfFile:pdfFileName];
    UIImage *image1 = [UIImage imageWithData:data];
    showImage.image = image1;
    return showImage;
}
我已研究并使用:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
我的代码已编辑如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellListsFriend *cell = [tableView dequeueReusableCellWithIdentifier:@"cellListsFriend" forIndexPath:indexPath];
    ContactAlphaB *contact = [self objectAtIndexPath:indexPath];
    [cell.lblRegisterType setTextColor:[SomeMethods_TrinhVM TrinhVM_colorWithHexString:@"777777"]];
    NSString *strNameImg = contact.linkAvatar;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
//        [NSURLConnection getImage:strNameImg showImage:cell.imgAvatarFriends];
        cell.lblNameFriend.attributedText = [NSString DrawColorForFullname_firstName:contact.firstName lastName:contact.lastName];
        cell.lblStatusSentense.text = contact.usernameIsUsing;
        [cell.lblStatusSentense setTextColor:[SomeMethods_TrinhVM TrinhVM_colorWithHexString:color_status_ShowUser]];
        if ([contact.registerType isEqualToString:@"1"]) {
            cell.lblRegisterType.text = @"Email";
        }
        else{
            cell.lblRegisterType.text = @"Mobile";
        }
        NSArray *arrayPaths =
        NSSearchPathForDirectoriesInDomains(
                                            NSDocumentDirectory,
                                            NSUserDomainMask,
                                            YES);
        NSString *path = [arrayPaths objectAtIndex:0];
        NSString* pdfFileName = [path stringByAppendingPathComponent:strNameImg];
        NSData *data = [NSData dataWithContentsOfFile:pdfFileName];
        UIImage *image1 = [UIImage imageWithData:data];
        cell.imgAvatarFriends.image = image1;
    });
    // Rounded Rect for cell image
    cell.imgAvatarFriends.layer.borderWidth=2;
    cell.imgAvatarFriends.layer.borderColor=[[UIColor whiteColor]CGColor];
    [cell.imgAvatarFriends.layer setCornerRadius:cell.imgAvatarFriends.frame.size.width/2];
    [cell.imgAvatarFriends.layer setMasksToBounds:YES];
    return cell;
}
它不会滞后,但会移动单元格的位置。如何解决这个问题

dispatch_async(queue, ^{
    cell.lblNameFriend.attributedText...
    etc.
}
有风险

看起来非常危险:您基本上是在事实发生后更改单元格的内容。您正在修改的单元格可能不是原始单元格,它可能已被重用。如果不使用
dequeueReusableCellWithIdentifier
,基本上允许系统缓存单元,并按照恰当的名称回收它们,那么这种方法就可以工作

安全

正确的方法与您所做的接近,但并不完全相同。您可以继续利用
dequeueReusableCellWithIdentifier
,但需要在数据(图像等)可用时触发重新加载单元格

换句话说,当您从返回单元格时:

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

您不能保留指向该单元格的指针以供以后使用。但是,当数据可用时,您可以根据需要多次要求系统重新绘制该单元格。

您是否看过在CellForRowatineXpath中做了这么多事情!图层属性(圆角矩形)将减慢向下滚动的速度。我可以想象数据跳来跳去,因为您使用的是一个异步方法,并且在再次调用它之前没有取消它。预加载数据并在cellForRowAtIndexPath中使用加载的数据。并将单元格逻辑移到您的CellListsFriend类中。您是如何解决您的问题的?