我有一个应用程序,我必须从互联网上获取信息。对 url 的请求在一个块中,这是在 UIViewController 出现之后进行的,这使得我的应用程序在运行时崩溃,因为我使用该信息来构造 UIViewController。
我不知道如何确保代码块完成他的任务并稍后使用信息。
*编辑*
现在我的应用程序显示一个空表,我不知道如何显示我得到的信息。也许可以使用 reloadTable,但我不知道如何。帮助!
StoreController.m
- (void)viewDidLoad
{
[super viewDidLoad];
_productData = [ProductData ProductData];
[_productData backEndTest];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_listOfChips count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductCellController *cell = (ProductCellController*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSString* nameNib = UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM() ? @"ProductCellController" : @"ProductCellControllerIphone";
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nameNib owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell = (ProductCellController*)[self cellFormat:cell atTheIndex:indexPath withThelastIndex:pathToLastRow];
cell.image.image = [UIImage imageNamed:[((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])image]];
cell.title.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])title]];
cell.price.text = [NSString stringWithFormat:@"$ %@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])price]];
cell.amount.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])quantity]];
}
产品数据.m
+(ProductData*)ProductData
{
ProductData* data = [[ProductData alloc]init];
return data;
}
- (void)backEndTest
{
[Server getProductsWithCompletionHandler:^(id result, NSError* error)
{
if ( error )
{
NSLog(@"error %@", error);
}
else
{
//NSMutableArray* arrayProducts = [NSMutableArray array];
int index = 0;
for (NSDictionary* product in result)
{
[_listOfProducts addObject:[[Products alloc] initWithProduct:[chip objectForKey:GSR_PARAM_PRODUCT_ID]
withTitle:[product objectForKey:GSR_PARAM_PRODUCT_DESCRIPTION]
numberUses:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
withPrice:[product objectForKey:GSR_PARAM_PRODUCT_PRICE]
withQuantity:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
withInAppID:[product objectForKey:GSR_PARAM_PRODUCT_IN_APP_ID]
withImage:[[self loadImages]objectAtIndex:index ]] ];
index++;
}
}
}];
}
我不知道如何管理街区并确保他完成。有任何想法吗?提前致谢!