iOS xCode - POST请求,JSON返回,TableCells

时间:2015-05-28 12:25:13

标签: ios json connection tableview

我遇到以下情况:我有一系列信标。我有一个php脚本,它根据发送到该脚本的信标的主要和次要值返回JSON个对象。

当我只有1个信标时,我的代码工作正常,因为只有1个请求。现在的问题是实际发生以下情况:

Request > Return JSON > Parse > Save in Array > Request (final)...Read out values in Tablecell.

有人可以帮我解决下一个请求只有在前一个请求被正确处理并添加到我的ObjectArray之后才能发送的情况吗?

因为现在我的第二个请求总是得到(null)结果。 (JSON中没有错误,因为hordcoded它确实有效。)

以下是我的一些代码:

连接制作:

for (int i = 0; i < self.beacons.count; i++) {
    if (self.beacons.count == 0) {
        NSLog(@"No beacons");
    }
    else {
        CLBeacon *beacon = (CLBeacon *)[self.beacons objectAtIndex:i];

        NSLog(@"Make beacon");
        self.majorValue = beacon.major;
        self.minorValue = beacon.minor;
        NSLog(@"beacon: MAJOR: %@. MINOR: %@", self.majorValue, self.minorValue);

        NSLog(@"Make Request");
        NSString *post = [NSString stringWithFormat:@"minor=%@&major=%@", self.minorValue, self.majorValue]; //POST naar php die returnd adhv deze waarden
        NSLog(@"REQUEST MWITHMAJOR: %@, EN MINOR %@: ", self.majorValue,  self.minorValue);

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://uploads.genicus.be/ruben/API/ArtworkData.php"]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
}

Connetion完成加载(这里我也有didReceiveData等,这里没有问题):

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *err;
//NSLog(@"error (%@",err);
self.artworkArray = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&err];
NSLog(@"Log ArtworkArray in ConnectionDidFinishLoading%@", self.artworkArray);
//NSLog(@"COUNTING: %lu", (unsigned long)self.artworkArray.count);

Artwork *artwork = [[Artwork alloc]init];
artwork.title = [self.artworkArray valueForKey:@"name"];
artwork.artist = [[self.artworkArray objectForKey:@"artist"] valueForKey:@"name"];
artwork.CreationYear = [self.artworkArray valueForKey:@"creationYear"];
artwork.categorie = [[self.artworkArray objectForKey:@"exposition"] valueForKey:@"name"];
artwork.image = [[[self.artworkArray objectForKey:@"images"] objectAtIndex:0] valueForKey:@"path"];

NSLog(@"artwork Title: %@", artwork.title);

NSLog(@"artworkObject: %@", artwork);
[self.savingBeaconSpecs addObject:artwork];
NSLog(@"SavingBeaconsSpecs count: %lu", (unsigned long)self.savingBeaconSpecs.count);


[self.tableView reloadData];

}

制作我的桌子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; //we willen de costumcell gebruiken en niet de standaard UItableCell

if (cell == nil) {
    cell = [[myTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

Artwork *artwork = (Artwork *)[self.savingBeaconSpecs objectAtIndex:indexPath.row];
NSLog(@"indexPath.row %ld", (long)indexPath.row);

NSLog(@"LOG ARTWORK IN TABLECELL%@", artwork);

cell.ArtworkTitle.text = artwork.title;
cell.ArtworkCreator.text = artwork.artist;
cell.ArtworkCategory.text = artwork.categorie;
cell.ArtworkYear.text = artwork.CreationYear;

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", artwork.image]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
cell.ArtworkImage.image = image;

return cell;

}

所以确切的问题:在null中记录ArtWorkArray时,我在第一个之后的所有请求都会导致(ConnectionDidFinishLoading)。第二个对象已经制作但实际上是空的......

日志的屏幕截图:Link to screenshot

0 个答案:

没有答案