Ios 使代码适应viewDidLoad而不是UITableView

Ios 使代码适应viewDidLoad而不是UITableView,ios,objective-c,Ios,Objective C,我以前在UITableView方法中调用过这段代码 -(UITableView单元格*)tableView:(UITableView*)tableView单元格for行索引路径:(nsindepath*)indepath 但是,我现在想在viewDidLoad中使用此代码,但当然这段代码不起作用,因为它使用的是objectatIndex:.row-我需要在代码中做哪些更改才能在viewDidLoad中工作 SKProduct * product = (SKProduct *) [_produc

我以前在UITableView方法中调用过这段代码

-(UITableView单元格*)tableView:(UITableView*)tableView单元格for行索引路径:(nsindepath*)indepath


但是,我现在想在viewDidLoad中使用此代码,但当然这段代码不起作用,因为它使用的是objectatIndex:.row-我需要在代码中做哪些更改才能在viewDidLoad中工作

SKProduct * product = (SKProduct *) [_products objectAtIndex:.row];

_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
labelPrice.text = [_priceFormatter stringFromNumber:product.price];

看起来您已经为价格格式化程序声明了iVar。如果是这种情况,请将此代码放入viewDidLoad:

_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
这更有效,因为我们可以为每个单元格使用相同的NSNumberFormatter,而不是一直创建新的

然后,您可以将此代码放入cellForRowAtIndexPath中:

SKProduct * product = (SKProduct *) [_products objectAtIndex:indexPath.row];
labelPrice.text = [_priceFormatter stringFromNumber:product.price];

只需将
indexPath.row
替换为所需对象的索引编号。例如,如果您想要第一个对象,只需使用:

SKProduct * product = (SKProduct *) [_products objectAtIndex:0];

仅供参考-请停止使用xcode标签,除非您的问题实际上是关于使用xcode。传递您想要获得所需产品的任何号码。如果您希望在
0
中第一次传递,而不是
indexath.row
。我不再需要使用UITableView,我正在尝试在UIViewControllery中执行此操作您可能可以使用_products.firstObject