2013-02-26 55 views
0

我已經用數組的數據創建了一個tableview。現在我想要詳細瞭解tableview的所有行。如果我從桌面視圖中選擇任何食譜,它應該向我展示所有的配料和程序,以準備該食譜與頂部的特定配方的圖片。我對這個領域很陌生,所以很有幫助。Tableview with details

這裏的 「ViewController.m」

#import "ViewController.h" 
@interface ViewController() 
@end 
@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
RecipeList = [[NSMutableArray alloc]init]; 
[RecipeList addObject:@"Chicken Kabab"]; 
[RecipeList addObject:@"Chicken Tikka"]; 
[RecipeList addObject:@"Chicken 65"]; 
[RecipeList addObject:@"Chicken Do Pyaza"]; 
CGRect frame = self.view.frame; 
RecipeTable = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain]; 
RecipeTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
RecipeTable.backgroundColor = [UIColor cyanColor]; 
RecipeTable.delegate = self; 
RecipeTable.dataSource = self; 
[self.view addSubview:RecipeTable]; 
[RecipeTable setScrollEnabled:YES]; 
[RecipeTable setUserInteractionEnabled:YES]; 
[RecipeTable setRowHeight:35]; 
} 
- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
return @"RECIPES"; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

return RecipeList.count; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
} 
cell.textLabel.text = [RecipeList objectAtIndex:indexPath.row]; 
return cell; 

} 
@end 
+0

如果我理解你的問題什麼你想在這裏得到你在桌面視圖中點擊的配方的詳細信息? – Meera 2013-02-26 06:21:02

+0

http://www.iphonesdkarticles.com/2009/01/uitableview-loading-detail-view.html – 2013-02-26 06:21:57

+0

@MeeraJPai是的。我只想要那張特別的菜的照片。 – Newbee 2013-02-26 06:42:28

回答

0

只需將食譜名稱傳遞給detailViewController即可。然後根據配方的名稱,您可以在UIImageView中加載合適的圖像。 在代碼中recipename是detailViewController中的一個NSString。聲明它作爲一個屬性..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
    NSString *name=[recipe objectAtIndex:indexPath.row]; 
     detailViewController.recipename=name; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 

} 

然後在detaiViewController.m

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     if([recipename isEqualToString:@"Pizza"]) 
     { 
      UIImage *image = [UIImage imageNamed: @"pizza.png"]; 
      [img setImage:image]; 
     } 
     else if([recipename isEqualToString:@"Chicken 65"]) 
     { 
      UIImage *image = [UIImage imageNamed: @"chicken.png"]; 
      [img setImage:image]; 
     } 
    } 
+0

我剛推了視圖,並沒有使用任何圖像,但是當我選擇行時,它不會去下一個視圖。你能指導我什麼是錯的。? – Newbee 2013-02-27 05:44:14

+0

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath DetailViewController * detailViewController = [[DetailViewController alloc] initWithNibName:@「DetailViewController」bundle:nil]; NSString * name = [RecipeList objectAtIndex:indexPath.row]; detailViewController.recipename = name; [self.navigationController pushViewController:detailViewController animated:YES]; } – Newbee 2013-02-27 06:07:25

+1

感謝您的幫助兄弟...做了一些改變,其工作正常... :) – Newbee 2013-02-27 09:31:36

0

必須使用的UITableView的DidSelectRow代表。然後,如果你選擇一個配方,它會調用DidSelectRow委託。在這個委託中,你執行推送到一個新的ViewController的編碼。在新的ViewController上創建一個UIImageView,然後將圖像從你的TableViewCell傳遞到你的新viewController的UIImageView

+0

你的意思是說,對於每一個食譜,我應該推新視圖控制器? – Newbee 2013-02-26 06:59:17

+0

使用相同的視圖控制器,但根據配方在視圖控制器上添加不同的視圖。用switch語句創建一個邏輯。 – 2013-02-26 10:01:08

0

如果你想要一個特定的Dish的細節在另一個ViewController中被推送,最簡單的方法是創建一個NSObject具有該特定菜所需的所有細節,並將其維護爲一個數組。當您在tableView中選擇一行時,didSelectRowAtIndexPath:將獲取該indexPath處的對象並將其傳遞給正在推送的下一個VC。在細節VC中,您可以使用對象中的細節填充細節。

而不是做這樣

RecipeList = [[NSMutableArray alloc]init]; 
[RecipeList addObject:@"Chicken Kabab"]; 

這樣做

RecipeList = [[NSMutableArray alloc]init]; 
[RecipeList addObject:dishObj]; 

其中dishObj是一個NSObject,其中含有的菜名,圖片和配方類菜的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    Dish *dishObject = [RecipeList objectAtIndex:indexPath.row]; 
    cell.textLabel.text = dishObject.dishName; 
    return cell; 
0

試試這個, 必須使用DidSelectRow代表的UITableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
// Navigation logic may go here. Create and push another view controller. 

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
NSString *strDishName = [RecipeList objectAtIndex:indexPath.row]; 
detailViewController.strDishDetail =strDishName; 
// ... 
// Pass the selected object to the new view controller. 
[self.navigationController pushViewController:detailViewController animated:YES]; 
} 
0

我希望以下教程將有助於你.. Segue

相關問題