如何根据行选择推送视图?

时间:2011-06-06 23:21:59

标签: iphone objective-c uitableview uinavigationcontroller uinavigationbar

enter image description here / *****更新** *** / Second Image enter image description here r.com/YH3cm.png

我想弄清楚上面的图片,我们如何知道用户是否选择了日期或跟踪。 / 已更新 / 我收到的数据是通过选择查询,我创建一个数组来存储列表。它是动态的,不必限于两个字段,它也可以有10个字段。我如何知道选择哪一行以及如何将数据推送到下一个视图。

与didSelectRowAtIndexPath类似,我应该如何在下一个视图上推送日期或轨道字段?

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

if (dvController == nil) 
  dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];

Teat *obj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];

dvController.obj = obj;
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:dvController animated:YES];

   }

3 个答案:

答案 0 :(得分:0)

我担心你不清楚自己想要做什么......如果你需要根据选择的行推出不同的视图,你可能会做一些像

这样的事情。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == 0) 
    //push view 1
else 
    //push view 2

}

更新:调用indexPath.row可以获得所选行的索引。我想你可以根据选择的行来决定做什么。要将此信息传递到下一个视图,您可以简单地想到要设置的@property字段,要调用的方法或要推送的视图控制器的自定义init方法。您发布的代码有什么问题?

答案 1 :(得分:0)

创建对象 - dateInfoViewController和trackInfoViewController,然后......

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

NSInteger row = [indexPath row];
if (row==0)
{
if (self.dateInfoViewController == nil)
     {
    DateInfoViewController *temp = [[DateInfoViewController alloc] init];
    self.dateInfoViewController = temp;
    [temp release];
    }



else {
    dateInfoViewController.title= [ NSString stringWithFormat:@"%@", [sessionInfoDetailsArray objectAtIndex:row]];
    YourAppDelegate *delegate = [[UIApplication sharedApplication]delegate];

    [delegate.sessionNavigationController pushViewController:dateInfoViewController animated:YES]; 
    }
}

  if (row==1)
{
    if (self.vetInfoViewController == nil)
    {
       TrackInfoViewController *temp = [[TrackInfoViewController alloc] init];
        self.trackInfoViewController = temp;
        [temp release];
    }


    else {
        trackInfoViewController.title= [ NSString stringWithFormat:@"%@", [sessionInfoDetailsArray objectAtIndex:row]];
        YourAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
        [delegate.sessionNavigationController pushViewController:trackInfoViewController animated:YES]; 
    }
}

答案 2 :(得分:0)

目前还不是很清楚你要做什么。如果你想根据单元格的内容推送某个视图控制器,但是没有明确的行排列,我会使用行索引来访问作为数据源的数组。一些非常宽松的代码:

WhateverObject* selectedObject= (WhateverObject*)[tableDataSourceArray objectAtIndex:indexPath.row];
if( [selectedObject hasAnAttributeYouCareAbout] )
{
    MyViewController* theCorrectController= whicheverViewControllerYouWant;
    theCorrectController.anAttribute= aValue;
    [self.navigationController pushViewController:theCorrectController animated:YES];
}

以下是如何使用特定属性定义UIViewController子类MyViewController的方法。在.h文件中:

@interface MyViewController : UIViewController {

     int anAttribute;
}

@property int anAttribute

@end

在.m文件中:

@implementation MyViewController

@synthesize anAttribute;

@end

您可以拥有任意类型的任意数量的属性,然后您可以使用aViewController.anAttribute设置它们。