1

申し訳ありませんが、IOS は初めてで、この問題の解決策がわかりませんでした

これは初心者向けのレストランメニューです

アイテムと価格を含むテーブルビューがあり、1 つのアイテムをクリックすると、ユーザーが数量を入力して完了ボタンをクリックする必要がある別のビューが表示されるので、ユーザーが完了をクリックすると、数量に価格を掛けたいと思います。その特定の価格を取得し、テキストフィールドにユーザーが入力した数量を掛けますか?

これが私のコードです

と呼ばれるメニューヘッダーファイルで NSDictionary を宣言しました

NSDictionary *dict;

私のviewdidloadメソッド

dict=[[NSDictionaryalloc]initWithObjectsAndKeys:
@"TomatoSoup",@"20.00",@"VegManchowSoup",@"12.00",nil];
NSLog(@"%@",dict);
[super viewDidLoad];

このコンテンツを表形式で表示しました

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
return [[dict allKeys]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

NSArray *sortedkeys=[[dict allKeys]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *key=[sortedkeys objectAtIndex:indexPath.row];
NSString *value=[dict objectForKey:key];
cell.textLabel.text=value;
cell.detailTextLabel.text=key;
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.row==0){   

VegQuantity *vegetarian1 = [[VegQuantity alloc]   initWithNibName:@"VegQuantity" bundle:nil];
vegetarian1.m_SelectedIndexPath=indexPath.row;
vegetarian1.pass=dict;
[self presentModalViewController:vegetarian1 animated:YES];
}
if(indexPath.row==1){   

VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];
vegetarian1.m_SelectedIndexPath=indexPath.row;
[self presentModalViewController:vegetarian1 animated:YES];
}
}

VegQuantity.h テキストフィールドと完了を示すボタンを持つビューがあります。完了ボタンをクリックすると、特定のスープの値を取得し、入力した数量で乗算する必要があります。私の問題は、その特定のキーの価格(値)を取得し、それを数量で乗算する方法です。

4

2 に答える 2

2
dict=[[NSDictionary alloc]initWithObjectsAndKeys:
                     @"TomatoSoup",@"20.00",@"VegManchowSoup",@"12.00",nil];

メソッドは initWithObjectsAndKeys です。これは、最初にオブジェクト、次にキー (キー "20.00"、オブジェクト - "TomatoSoup") であることを意味します - あなたの場合は逆です。

次に、価格の NSString (価格または数量だと思います) の代わりに、NSNumber - [NSNumber numberWithFloat:20.0f] を使用します。

次に、VegQuantity ビュー コントローラーを作成します (命名規則を維持するために、VegQuantityViewController と呼ぶことをお勧めします) 2 つのプロパティ:

@property (nonatomic, strong) NSString *itemName; //Use strong if using ARC,  otherwise retain
@property (nonatomic, strong) NSNumber *price;

それらの値を表示する前にView Controllerに渡します。その中で、あなたは彼らとやりたいことを何でもすることができます。PS プロパティを使用してインスタンス変数の値を操作することをお勧めします。

于 2012-08-24T07:04:51.423 に答える
0

を使用して Dictionary から値を取得します。

[dict objectForKey:@"someDummyKey"];

しかし、正直に言うと。NSDictionary ではなく、UITableView のデータソースとして NSMutableArray を使用する必要があります。

于 2012-08-24T06:55:01.153 に答える