我正在构建一个 iOS 应用程序,我希望允许人们从在我的数据库中注册的商店订购东西。为了让我的用户查看产品、公司等,我需要下载包含请求信息的数组。我就是这样做的:
- (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];
}
该getOrderItems
方法是我为每个订单拥有的自定义类中的一个方法。当用户选择一个订单时,该方法被调用以下载属于该订单的产品。现在这个过程一切正常,但问题是当我的 TableView 已经设置好自己时,数据会被接收。
我想添加一个观察者来检查这个数组中的产品数量是否发生变化,如果发生变化,我想更新 tableview。我已经在谷歌上搜索了几个小时,但对此并没有说太多,对我没有任何用处。
任何帮助将非常感激!先感谢您