我很抱歉,我是新來的IOS,我不能找出解決這個問題字典檢索關鍵IOS
這只是一個初學者餐廳的菜單
有包含項的tableview和價格,當我點擊一個項目時,它會顯示另一個視圖,用戶必須輸入數量並單擊完成按鈕,因此當用戶點擊完成時,我想將數量乘以價格,我如何檢索該特定價格並請將其與文本字段中的數量用戶輸入相乘。
這裏是我的代碼
我已經叫
NSDictionary *dict;
我viewDidLoad方法
dict=[[NSDictionaryalloc]initWithObjectsAndKeys:
@"TomatoSoup",@"20.00",@"VegManchowSoup",@"12.00",nil];
NSLog(@"%@",dict);
[super viewDidLoad];
我已經在表視圖中顯示該內容菜單頭文件中聲明的NSDictionary
- (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 有一個視圖有一個文本框和一個按鈕說完成,現在當我點擊完成按鈕時,我需要檢索該特定湯的值,並將其與輸入的數量相乘。 我的問題是我該如何檢索該特定鍵的價格(價值)並將其與數量相乘。
如果我使用NSMutableArray我如何檢索特定湯的特定價格 – ipack26
您將不得不爲每道菜製作一本詞典。包含鍵值對和鍵名和相應的值。 然後在'didSelectRowAtIndexPath'中,您只需將包含在數組中的字典傳遞給您的veggi-class。您將通過使用'indexPath.row'知道哪個數組。在Veggi-Class內部,您可以訪問正確的培養皿並使用數據。 – Maverick1st
您只需訪問textfield.text即可獲得文本字段的值。您不應該忘記從字典和文本字段中包含的字符串中創建浮點值。否則你的乘法會失敗。 :) – Maverick1st