如何从一个表视图到另一个表视图

时间:2014-08-06 09:02:20

标签: ios uitableview

我是初学者,我正在尝试创建一个应用程序,您可以从中选择多个选项。因此,我有一个表格视图。根据点击的单元格,另一个屏幕应显示第二个表视图,我可以在其中勾选标记。现在,我有一个视图控制器,我正在创建第一个表视图。如何进入第二个表视图以及它如何显示另一个数组。这是迄今为止的代码......

@interface ViewController (){

  NSArray *genres;
  NSArray *images;
  NSMutableArray *array1;
  NSMutableArray *array2;
  NSMutableArray *array3;
}
@end

@implementation ViewController

@synthesize tableView;

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
  return self;
}

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

  self.title = @"Genres";

  // Arrays must be in same order for image and title correspondance

  genres = [[NSArray alloc] initWithObjects:
            @"a1",
            @"a2",
            @"a3",
            nil];

  images = [[NSArray alloc] initWithObjects:
            @"1.jpeg",
            @"2.jpeg",
            @"3.jpeg",
            nil];

  array1 = [[NSMutableArray alloc] initWithObjects:
            @"1.1",
            @"1.2",
            @"1.3",
            nil];

  array2 = [[NSMutableArray alloc] initWithObjects:
            @"2.1",
            @"2.2",
            @"2.3",
            nil];

  array3 = [[NSMutableArray alloc] initWithObjects:
            @"3.1",
            @"3.2",
            @"3.3",
            nil];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
  return 1;
}

- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
  return genres.count;
}

//Customize the apperance of table view cells

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

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
      reuseIdentifier:CellIdentifier];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      }
  }

//Configure Cell

cell.textLabel.text = [genres objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
return cell;
}

#pragma mark -
#pragma mark Table view delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  NSArray *selectedArray = [[NSArray alloc] init];

  switch (indexPath.row) {
    case 0:
        selectedArray = array1;
        break;
    case 1:
        selectedArray = array2;
        break;
    case 2:
        selectedArray = array3;
        break;        
    default:
        break;
  }
}

1 个答案:

答案 0 :(得分:1)

您可以1)从代码创建第二个视图控制器并设置数组像参数或2)使用segue接口:

尝试按照以下完整答案:  https://stackoverflow.com/a/9736559/2429147

此链接对于使用segues和storyboards也很有帮助: http://agilewarrior.wordpress.com/2012/01/25/how-segue-in-ios-and-pass-data-from-one-viewcontroller-to-another/