سؤال

I am building an iOS app in which I want to allow people to order stuff from stores that are registered in my database. To allow my users to see products, companies, etcetera, I need to download array's with the requested information. This is how I do that:

- (void)getOrderItems {
    _dbAction = @"getOrderItems";
    NSString *post = [NSString stringWithFormat:@"controller=%@&action=%@&orderNumber=%@", @"BakkerFunctions", @"getOrderItems", self.orderNumber];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];


  NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://www.mysite.nl/API/"];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    [request setHTTPBody:postData];
    [request setURL:url];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection could not be made");
    }
    }

}   

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {

        NSArray *orderItemsFromDatabase = [[NSArray alloc] init];

            orderItemsFromDatabase = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL][@"itemsFromDatabase"];
            NSString *datastring = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
            NSLog(@"%@ Array:%@", datastring, orderItemsFromDatabase);

            //convert array from database in array of custom objects
            NSMutableArray *orderItems = [[NSMutableArray alloc] init];
            for (int i = 0; i < orderItemsFromDatabase.count; i++) {
                [orderItems addObject:
                 [[Item alloc] initWithItemName:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemName"]
                                itemDescription:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemDescription"]
                                       itemCode:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemCode"]
                                    itemBarCode:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemBarCode"]
                                      itemPrice:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemPrice"]
                                   itemQuantity:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemQuantity"]
                                 itemUserRemark:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemUserRemark"]
                                    itemCompany:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"itemCompany"]
                             productImageNumber:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"productImageNumber"]
                                       category:[[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"category"]
                                      itemImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.imagine-app.nl/ProductImages/%@%@", [[orderItemsFromDatabase objectAtIndex:i]objectForKey:@"productImageNumber"], @".jpg"]]]]]];
            }

            self.orderItems = [NSArray arrayWithArray:orderItems];
    }

the getOrderItems method is a method inside the custom class I have for each order. when the user selects an orders, this method gets called to download the products that belong to this order. Now this process works all fine, but the problem is that the data gets receiver when my TableView already set itself up.

I want to add an observer to check if the amount of products in this array changes, and if it does, i want to update the tableview. I have searched for hours on SO an google, but not much is said about this and nothing useful to me.

any help would be much appreciated! thank you in advance

هل كانت مفيدة؟

المحلول 3

I'm not adding an observer anymore, As soon as my custom class finished processing the received data, it calls the orders view controller. That made that the data is loaded before the tableview is set up and the problem is solved.

نصائح أخرى

A simple solution would be to use a MBProgressHUD or a similar control which will display a message saying that please wait while the data is being downloaded. Once, the data is downloaded it will trigger a callback meaning that it has downloaded the data and then you can refresh your UITableView to reflect all the data.

Override the orderItems setter and reload the table in there.

- (void)setOrderItems:(NSArray*)items
{
    _items = items;
    [self.tableView reloadData];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top