11

我在View Controller内部有一个表视图。我可以在表格视图中填充所有信息。不过,我设置详细视图有点失落。我相信每个表格单元格都需要一个细节视图,但不完全确定。使用Xcode Storyboard推送到表视图细节视图

这是我的代码。我错过了什么来完成从表格视图到细节视图的延续? 代码:

.h 

@interface DetailViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> 
{ 
    IBOutlet UITableView *myTable; 
    NSMutableArray *contentArray; 
} 

@property (strong, nonatomic) IBOutlet UITableView *myTable; 

.m 


- (void)viewDidLoad 
{ 
    contentArray = [[NSMutableArray alloc]init]; 
    [contentArray addObject:@"Espresso"]; 
    [contentArray addObject:@"Latte"]; 
    [contentArray addObject:@"Capicino"]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [contentArray count]; 
} 

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if([[contentArray objectAtIndex:indexPath.row]isEqualToString:@"EspressoViewController"]) 
    { 
     EspressoViewController *espresso = [[EspressoViewController alloc]initWithNibName:@"EspressoViewController" bundle:nil]; 
     [self.navigationController pushViewController:espresso animated:YES]; 
    } 
    else if ([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"Latte"]) 
    { 
     LatteViewController *latte = [[LatteViewController alloc] initWithNibName:@"Latte" bundle:nil]; 
     [self.navigationController pushViewController:latte animated:YES]; 
    } 

} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    [self tableView:tableView didSelectRowAtIndexPath:indexPath]; 
} 


- (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"]; 
    } 

    NSString *cellValue = [contentArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.textLabel.font = [UIFont systemFontOfSize:16]; 
    cell.detailTextLabel.text = @"Hot and ready"; 

    UIImage *image = [UIImage imageNamed:@"coffeeButton.png"]; 
    cell.imageView.image = image; 

    cell.textLabel.text = [contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

回答

17

我想你让这有点太复杂。别担心,我经常做同样的事情。

首先,从tableView:accessoryButtonTappedForRowAtIndexPath:发送tableView:didSelectRowAtIndexPath:这两种方法没有区别。点击单元格,或者它的附件按钮执行相同的操作。如果您不需要附件按钮来执行与单击单元格本身不同的操作,请将其移除。其次,如果您使用的是故事板,则不需要为视图控制器分配/ initWithNib。相反,使用segue。如果你这样做,通过故事板,你也不需要编程推viewControllers到您的navigationController

首先建立你的故事板:

  1. 拖出一个UITableViewController。确保使用右侧的检查器窗格将您拖出的UITableViewController类设置为您自己的“DetailViewController”。
  2. 然后选择该控制器和使用该菜单中选择“编辑 - >嵌入在 - >导航控制器”。
  3. 接下来,拖出三个通用的UIViewControllers。将一个类设置为“LatteViewController”,将另一个设置为“EspressoViewController”,将第三个类设置为“CapicinoViewController”(再次使用检查器)。
  4. 控制+从UITableViewController拖动到每个这些viewControllers并选择PUSH
  5. 单击你的UITableViewController和每个viewControllers之间的箭头上的小圆圈。在检查员(右侧)中,为每个segue在标识符字段中指定一个唯一的名称。你需要记住你的代码的名字。我会将它们命名为“EspressoSegue”,“LatteSegue”和“CapicinoSegue”。你会在下面的代码中看到为什么。

然后把下面的代码在你的UITableViewController:

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

//Build a segue string based on the selected cell 
NSString *segueString = [NSString stringWithFormat:@"%@Segue", 
         [contentArray objectAtIndex:indexPath.row]]; 
//Since contentArray is an array of strings, we can use it to build a unique 
//identifier for each segue. 

//Perform a segue. 
[self performSegueWithIdentifier:segueString 
          sender:[contentArray objectAtIndex:indexPath.row]]; 
} 

你如何实现剩下的就是你。您可能想要在您的UITableViewController中实现prepareForSegue:sender:,然后使用该方法将信息发送到segue.destinationViewController

请注意,我将您的contentArray中的字符串作为segue的发件人。你可以传递你喜欢的任何东西。标识单元格的字符串看起来像是要传递的最合乎逻辑的信息,但是选择取决于您。

上面贴出的代码应该为您执行导航。

相关问题