2014-02-17 228 views
1

嗨,我正在做一個食物的應用程序,我有一個問題。我有一個收集視圖控制器有10個項目(每個是不同的菜)。這些項目中的每一個都有一個圖像和一個標題,我想要做的是,當有人點擊該項目(菜)時,它將用戶帶到一個桌面視圖控制器,該控制器有關於該菜餚的項目(成分)列表。我的問題是,我不知道如何將數據從集合視圖控制器傳遞到表視圖控制器。這裏是我的代碼:iOS將數據收集視圖控制器傳遞給表視圖控制器

***View Controller.m (collection view controller):*** 

@interface ViewController() 
{ 
    NSArray *_arrayOfImages; 
    NSArray *_arrayOfNames; 

    **// Array de ingredientes** 
    NSArray *_arrayOfCeviche; 
    NSArray *_arrayOfBrasa; 
    NSArray *_arrayOfTacacho; 
    NSArray *_arrayOfPachamanca; 
    NSArray *_arrayOfAnticucho; 
    NSArray *_arrayOfChaufa; 
    NSArray *_arrayOfPapita; 
    NSArray *_arrayOfParihuela; 
    NSArray *_arrayOfLomo; 
    NSArray *_arrayOfCuy; 

    **//Array which stores all the ingredients arrays** 
    NSArray *_arrayOfIngredients; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

**// Array which contain the titles** 
_arrayOfNames = @[@"Anticucho", @"Arroz Chaufa", @"Cebiche", @"Pachamanca", @"Papa a la    Huancaina", @"Parihuela", @"Picante de cuy", @"Tacacho con cecina", @"Pollo a la brasa", @"Lomo saltado"]; 

**// Array which containe the images** 
_arrayOfImages = @[@"Anticucho.jpg", @"chaufa.jpg", @"ceviche.jpg", @"pachamanca.jpg", @"papita.jpg", @"parihuela.jpg", @"cuy.jpg", @"tacacho.jpg", @"brasa.jpg", @"lomo.jpg"]; 

// Arrays que de los ingredientes 
_arrayOfCeviche = @[@"Quesito", @"Limon", @"Choclito", @"Cocinita", @"La lechesita", @"La miradita", @"La weuabdita", @"La cagada", @"Ptm Toy Jodido XD", @"Que emoción", @"Wiiiiiii"]; 

_arrayOfBrasa = @[@"jamonadita", @"delicisoso", @"Sexo", @"Anal", @"Oral", @"Vaginal", @"Culantraso", @"Violenita", @"Sexo salvaje", @"Mujeres", @"Coger coger"]; 

_arrayOfChaufa = @[@"qUESIto", @"Jodido", @"Ala", @"Que pajita", @"Wuauau", @"Tengo hambrunita", @"jojolete", @"Sequito duro", @"Latititititito", @"Manusico"]; 

**// Array which contains the other arrays** 
_arrayOfIngredients = @[@"_arrayOfBrasa, _arrayOfCeviche,_arrayOfChaufa]; 

} 

**// Method to pass the data from a collection view controller to a table view controller** 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

if ([segue.identifier isEqualToString:@"showDetail"]) { 

    NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0]; 

    DetailViewController *dvc = [segue destinationViewController]; 
    dvc.Titulos = [_arrayOfIngredients objectAtIndex:indexPath.row]; 
} 
} 

而我的表視圖控制器代碼:

***Detail View Controller.m (table view controller):*** 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

// Configure the cell... 
cell.textLabel.text = self.Titulos; 

return cell; 
} 

我最大的問題是包含其他數組的數組。嘗試訪問_arrayOfIngredients並從中訪問所選項目的成分數組。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    **// Array which contain the titles** 
     _arrayOfNames = @[@"Anticucho", @"Arroz Chaufa", @"Cebiche", @"Pachamanca", @"Papa a la Huancaina", @"Parihuela", @"Picante de cuy", @"Tacacho con cecina", @"Pollo a la brasa", @"Lomo saltado"]; 

    **// Array which containe the images** 
     _arrayOfImages = @[@"Anticucho.jpg", @"chaufa.jpg", @"ceviche.jpg", @"pachamanca.jpg", @"papita.jpg", @"parihuela.jpg", @"cuy.jpg", @"tacacho.jpg", @"brasa.jpg", @"lomo.jpg"]; 

     **// Arrays que de los ingredientes** 
     _arrayOfCeviche = @[@"Quesito", @"Limon", @"Choclito", @"Cocinita", @"La lechesita", @"La miradita", @"La weuabdita", @"La cagada", @"Ptm Toy Jodido XD", @"Que emoción", @"Wiiiiiii"]; 

     _arrayOfBrasa = @[@"jamonadita", @"delicisoso", @"Sexo", @"Anal", @"Oral", @"Vaginal", @"Culantraso", @"Violenita", @"Sexo salvaje", @"Mujeres", @"Coger coger"]; 

     _arrayOfChaufa = @[@"qUESIto", @"Jodido", @"Ala", @"Que pajita", @"Wuauau", @"Tengo hambrunita", @"jojolete", @"Sequito duro", @"Latititititito", @"Manusico"]; 

     **// Array which contains the other arrays** 
     _arrayOfIngredients = @[@"_arrayOfBrasa, _arrayOfCeviche,_arrayOfChaufa"]; 

    } 

請幫我解決這個問題:(

回答

0

Collection視圖有一個委託方法didSelectItemAtIndexPath:當用戶選擇項目的時候觸發使用它可以通過調用SEGUE和傳遞數據:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"showDetail" sender: indexPath]; 
} 

和在prepareForSegue:傳遞數據:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

if ([segue.identifier isEqualToString:@"showDetail"]) { 

    NSIndexPath *indexPath = (NSIndexPath*)sender; 

    DetailViewController *dvc = [segue destinationViewController]; 
    dvc.Titulos = [_arrayOfIngredients objectAtIndex:indexPath.row]; 
} 
} 
相關問題