不使用PFQueryTableView

时间:2015-12-10 08:11:47

标签: ios uitableview parse-platform

我正在使用UITableViewController来显示来自Parse的数据。它在我的Xcode Simulator上完美运行,因为我认为网络中没有延迟。但每当我将代码上传到AppStore for Testing时。我第一次运行应用程序时,必须从Parse加载几个餐厅并在UITableViewController中显示。在点击一行时,第一行数据被加载到第三行,第四行数据不规则地加载到第六行数据中。为什么数据加载非常不均匀?这是我的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier  = @"restaurantIdentifier";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    PFObject *tempObject = [self.objectArray objectAtIndex:indexPath.row];

    PFFile *imageFile = [tempObject objectForKey:@"RestaurantIcon"];
    PFImageView *imageView = [[PFImageView alloc] init];
    imageView.file = imageFile;

    [imageView loadInBackground:^(UIImage *img,NSError *error){

        if(!error){
            cell.imageCell.image = imageView.image;
        }
    }];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 4;

        cell.imageView.frame = self.view.bounds;
        cell.cellLabel.text = [tempObject objectForKey:@"RestaurantName"];
        [self.hotelNamesArray addObject:[tempObject objectForKey:@"RestaurantName"]];

        cell.cellLabel.lineBreakMode = NSLineBreakByWordWrapping;

    return cell;
}




- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    _restaurantName = [self.hotelNamesArray objectAtIndex:indexPath.row];

        self.restaurantMenuNameArray = [[NSMutableArray alloc] init];

        PFQuery *query = [PFQuery queryWithClassName:[self.hotelNamesArray objectAtIndex:indexPath.row]];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

                    if (!error) {
            for (PFObject *obj in objects) {

                if (![_restaurantMenuNameArray containsObject:[obj objectForKey:@"RestaurantMenuName"]]) {

                    NSLog(@"restaurantmenunames are %@",[obj objectForKey:@"RestaurantMenuName"]);

                    if ([obj objectForKey:@"RestaurantMenuName"] ==nil) {
                        [self performSegueWithIdentifier:@"restaurantDetail" sender:self];
                        return;
                    }else {
                        [_restaurantMenuNameArray addObject: [obj objectForKey:@"RestaurantMenuName"]];
                    }
                }
            }
        }else {
                // Log details of the failure
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
            [self.tableView reloadData];
            NSLog(@"restaurantMenuNames is %@",_restaurantMenuNameArray);
            [self performSegueWithIdentifier:@"restaurantDetail" sender:self];
        }];
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您的意思是图像进入了错误的单元格,则必须考虑滚动时单元格是否被回收,如果图像加载时间过长,则可能会在重复使用单元格后得到结果。

您需要检查单元格是否仍然是您想要的项目/行(您可以将行存储在单元格中tag并在完成处理程序中设置图像之前检查它,实例)。

如果其他数据混淆了,那么您需要向我们展示加载该数据的代码。