Ios 在UITableView中,从服务器加载时切换第一项和最后一项记录数据

Ios 在UITableView中,从服务器加载时切换第一项和最后一项记录数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在开发一个应用程序,可以在视差型UITableView中显示来自服务器的数据,下面是我的代码。一切都很好,但细胞数据图像等保持从一个细胞切换到另一个细胞 - (void)viewDidLoad { [self hasInternet]; self.tableView.dataSource = self; self.tableView.delegate = self; [self loadData]; self.edgesForExtendedLayou

我正在开发一个应用程序,可以在视差型UITableView中显示来自服务器的数据,下面是我的代码。一切都很好,但细胞数据图像等保持从一个细胞切换到另一个细胞

- (void)viewDidLoad
{
    [self hasInternet];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self loadData];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self scrollViewDidScroll:nil];
    [super viewWillAppear:animated];
    [self loadData];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}
- (void) loadData{
    name = @"name";
    email = @"email";
    thumbnail = @"thumbnail";
    myObject = [[NSMutableArray alloc] init];
    NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://URL.php"]];
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dataDict in jsonObjects) {
        NSString *title_data = [dataDict objectForKey:@"fname"];
        NSString *title_data2 = [dataDict objectForKey:@"lname"];
        NSString *fulname = [NSString stringWithFormat:@"%@ %@", title_data, title_data2];
        NSString *emAil = [dataDict objectForKey:@"email"];
        NSString *thumbnail_data = [dataDict objectForKey:@"img"];
        thumbnail_data = [NSString stringWithFormat:@"http://URL/upload/%@",thumbnail_data];
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys: fulname, name, emAil, email, thumbnail_data, thumbnail, nil];
        [myObject addObject:dictionary];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"parallaxCell";
    JBParallaxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[JBParallaxCell alloc]initWithStyle:
              UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
    NSMutableString *text;
    text = [NSMutableString stringWithFormat:@"%@",[tmpDict objectForKeyedSubscript:name]];
    NSMutableString *mail;
    mail = [NSMutableString stringWithFormat:@"%@",[tmpDict objectForKeyedSubscript:email]];
    NSMutableString *images;
    images = [NSMutableString stringWithFormat:@"%@ ",[tmpDict objectForKey:thumbnail]];
    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.parallaxImage.image = [[UIImage alloc]initWithData:data];
        });
    });
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",text];
    cell.subtitleLabel.text = [NSString stringWithFormat:@"%@",mail];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray *visibleCells = [self.tableView visibleCells];    
    for (JBParallaxCell *cell in visibleCells) {
        [cell cellOnTableView:self.tableView didScrollOnView:self.view];
    }
}

当我编译它时,它会显示我所有的数据,但会不断地从一个单元格切换到另一个单元格。任何帮助都将不胜感激。谢谢

,因为UITableView重用了单元格,所以当您向下或向上滚动时,从tableview绑定的上一个单元格将被重用。但它的图像视图在上次使用时有一个图像,所以您必须首先清除图像视图。 所以把这条线

cell.parallaxImage.image = nil;
在调度队列之前

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

您可以使用此代码,它将图像保存在文档目录中,然后显示它。 请更改变量名

NSString *imageURL = [[matcheListArr objectAtIndex:indexPath.row] valueForKey:@"thumbnail_image"];
NSURL *url = [NSURL URLWithString:imageURL];
NSArray *seperate = [[[matcheListArr objectAtIndex:indexPath.row] valueForKey:@"thumbnail_image"] componentsSeparatedByString:@"/"];
NSString *fileName = [NSString stringWithFormat:@"%@.png",[seperate objectAtIndex:seperate.count-1]];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *getImagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
if ([img isKindOfClass:[UIImage class]]) {
    //Set Downloaded Image
    [cell.matchUserImageView setImage:img];
}
else {
    if ([[ValidationString sharedManager] isNullString:[[matcheListArr objectAtIndex:indexPath.row] valueForKey:@"thumbnail_image"]] == YES) {
        //Set Default Image
        [cell.matchUserImageView setImage:[UIImage imageNamed:@"photo_icon.png"]];
    }
    else{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            NSData *imageData = [NSData dataWithContentsOfURL:url];
            NSArray *seperate = [[[matcheListArr objectAtIndex:indexPath.row] valueForKey:@"thumbnail_image"] componentsSeparatedByString:@"/"];
            NSString *fileName = [NSString stringWithFormat:@"%@.png",[seperate objectAtIndex:seperate.count-1]];

            dispatch_async(dispatch_get_main_queue(), ^{
                // Update the UI
                [cell.matchUserImageView setImage:[UIImage imageWithData:imageData]];
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
                NSString *savedImagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
                [imageData writeToFile:savedImagePath atomically:NO];
            });
        });
    }
}

虽然这样做了,但没有什么区别,还是一样的问题。谢谢你的回答。你能详细解释一下你的问题吗?你在cell=[UITableViewCell alloc]中还有另一个问题。这将是[[JBParallaxCell alloc]init]的问题,在从服务器记录加载uitableview中的数据后,保持自己从一个单元格切换到另一个单元格。您是否更改了单元格alloc init?旁注:异步加载是一件好事,但您需要检查它是否仍然是在该单元格上设置的适当内容,或者,您将填充一个应用于不同内容的单元格。如果[[ValidationString sharedManager]为空字符串:[[Matchelistar objectAtIndex:indexPath.row]valueForKey:@thumbnail\u image]==是,则ValidationString在其中是什么