11

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

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

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

1 回答 1

17

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

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

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

首先构建你的故事板:

  1. 拖出一个 UITableViewController。确保使用右侧的检查器窗格将拖出的 UITableViewController 的类设置为您自己的“DetailViewController”。
  2. 然后选择这个控制器并使用菜单选择“ Editor -> Embed In -> Navigation Controller ”。
  3. 接下来,拖出三个通用的 UIViewController。将一个的类设置为“LatteViewController”,将另一个设置为“EspressoViewController”,将第三个设置为“CapicinoViewController”(再次使用检查器)。
  4. Control+从 UITableViewController 拖动到每个 viewController 并选择PUSH
  5. 单击 UITableViewController 和每个视图控制器之间箭头上的小圆圈。在检查器(右侧)中,在Identifier字段中为每个 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]];
}

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

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

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

于 2012-09-24T04:10:57.700 回答