我正在构建一个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。我已经在谷歌搜索了几个小时,但对此没有太多说法,对我来说没什么用。
任何帮助将不胜感激!提前谢谢你
答案 0 :(得分:0)
一个简单的解决方案是使用MBProgressHUD或类似的控件,它会显示一条消息,说明请在下载数据时等待。一旦数据被下载,它将触发回调,这意味着它已经下载了数据,然后您可以刷新UITableView以反映所有数据。
答案 1 :(得分:0)
覆盖orderItems
setter并在那里重新加载表。
- (void)setOrderItems:(NSArray*)items
{
_items = items;
[self.tableView reloadData];
}
答案 2 :(得分:0)
我不再添加观察者了,一旦我的自定义类完成处理接收到的数据,它就会调用orders视图控制器。这样就可以在设置tableview之前加载数据并解决问题。