ios 使用 Xcode Storyboard 从表视图单元格推送到详细信息视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12464164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:12:46  来源:igfitidea点击:

Pushing to a Detail View from a Table View Cell using Xcode Storyboard

objective-ciosuiviewcontrolleruistoryboardsegueuitableview

提问by flyers

I have a table view inside a View Controller. I can populate all my information inside the table view. However I am a bit lost for setting up the detail views. I believe each table cell needs a segue to a each detail view but not completely sure.

我在视图控制器中有一个表视图。我可以在表格视图中填充我的所有信息。但是,我对设置细节视图有点迷茫。我相信每个表格单元格都需要转至每个细节视图,但并不完全确定。

Here is my code. What am I missing to accomplish the segue from the table view to the detail views? Code:

这是我的代码。完成从表视图到详细视图的转场,我缺少什么?代码:

.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;
}

回答by JoeBob_OH

I think you made this a little too complicated. Don't worry, I do the same thing often.

我觉得你把这弄得太复杂了。别担心,我经常做同样的事情。

First, by sending tableView:didSelectRowAtIndexPath:from within tableView:accessoryButtonTappedForRowAtIndexPath:there is no difference between the two methods. Tapping the cell, or it's accessory button performs the same action. If you don't need the accessory button to perform a different action than tapping the cell itself, remove it.

首先,通过tableView:didSelectRowAtIndexPath:从内部发送,tableView:accessoryButtonTappedForRowAtIndexPath:这两种方法没有区别。点击单元格或其附件按钮执行相同的操作。如果您不需要附件按钮来执行与点击单元格本身不同的操作,请将其移除。

Second, if you're using a storyboard, you do not need to alloc/initWithNib for your view controllers. Instead, use a segue. If you do this through the storyboard, you also don't need to programmatically push viewControllers onto your navigationController

其次,如果您使用的是故事板,则不需要为视图控制器分配/initWithNib。相反,使用 segue。如果您通过故事板执行此操作,您也不需要以编程方式将 viewControllers 推送到您的 navigationController

Build your storyboard first:

首先构建你的故事板:

  1. Drag out a UITableViewController. Make sure you set the class of the UITableViewController you dragged out to your own "DetailViewController" using the inspector pane on the right side.
  2. Then select this controller and using the menus choose "Editor->Embed In->Navigation Controller".
  3. Next, drag out three generic UIViewControllers. Set the class of one to "LatteViewController", another to "EspressoViewController", and a third to "CapicinoViewController" (using the inspector again).
  4. Control+drag from the UITableViewController over to each of these viewControllers and choose PUSH.
  5. Click on the little circle that's on the arrow between your UITableViewController and each of these viewControllers. In the inspector (on the right side), give each segue a unique name in the Identifierfield. You will need to remember this name for your code. I would name them "EspressoSegue", "LatteSegue", and "CapicinoSegue". You'll see why in the code below.
  1. 拖出一个 UITableViewController。确保使用右侧的检查器窗格将拖出的 UITableViewController 的类设置为您自己的“DetailViewController”。
  2. 然后选择此控制器并使用菜单选择“编辑器->嵌入->导航控制器”。
  3. 接下来,拖出三个通用 UIViewController。将一个类设置为“LatteViewController”,另一个设置为“EspressoViewController”,第三个设置为“CapicinoViewController”(再次使用检查器)。
  4. Control+从 UITableViewController 拖动到每个 viewControllers 并选择PUSH
  5. 单击 UITableViewController 和每个视图控制器之间箭头上的小圆圈。在检查器(右侧)中,在Identifier字段中为每个 segue 指定一个唯一名称。您需要记住此代码的名称。我将它们命名为“EspressoSegue”、“LatteSegue”和“CapicinoSegue”。您将在下面的代码中看到原因。

Then put the following code in your UITableViewController:

然后将以下代码放入您的 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]];
}

How you implement the rest is up to you. You may want to implement prepareForSegue:sender:in your UITableViewController and then use that method send information over to segue.destinationViewController.

您如何实施其余部分取决于您。您可能希望prepareForSegue:sender:在 UITableViewController 中实现,然后使用该方法将信息发送到segue.destinationViewController.

Note that I passed the string from your contentArray as the sender for the segue. You can pass whatever you like. The string that identifies the cell seems like the most logical information to pass, but the choice is up to you.

请注意,我从您的 contentArray 中传递了字符串作为 segue 的发送者。你可以通过任何你喜欢的。标识单元格的字符串似乎是最合乎逻辑的信息传递,但选择取决于您。

The code posted above should perform the navigation for you.

上面发布的代码应该为您执行导航。