iOS将数据收集视图控制器传递给表视图控制器

时间:2014-02-17 14:47:10

标签: ios objective-c

嗨,我正在做一个食物应用程序,我遇到了问题。我有一个集合视图控制器,有10个项目(每个是一个不同的菜)。这些项目中的每一个都有一个图像和一个标题,我想要做的是当有人点击该项目(菜肴)时,它将用户带到一个桌面视图控制器,该控制器有一个关于菜肴的项目(成分)列表。我的问题是我不知道如何将数据从集合视图控制器传递到表视图控制器。这是我的代码:

***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"];

    }

请帮我解决这个问题:(

1 个答案:

答案 0 :(得分: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];
 }
}