我如何在详细视图中知道tableview中的哪个单元格被选中?

时间:2010-05-20 12:42:19

标签: iphone xcode uitableview didselectrowatindexpath

我怎么知道选择了哪个tableview单元?(在详细视图中) 问题是。 我有一个表视图控制器。这里是从互联网条目到表格解析。 所以这是一个从互联网加载的动态tabe视图。我不知道表中会有多少条目,所以当我点击一行时,我不知道要调用的详细信息视图。 所以我提出了一个观点。此视图包含日历。在这个日历上(详细信息),我将根据所选行解析来自互联网的数据。 例如:我有表:条目1,条目2,条目3,条目4 当我点击条目2时,我需要用参数条目2调用一个php.php将知道我选择的表上的条目,并将生成我将解析的正确的xml。

这是我的tableview didSelectRow函数:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

 if(bdvController == nil)
    bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]];
  Villa *aVilla = [appDelegate.villas objectAtIndex:indexPath.row];

  [self.navigationController pushViewController:bdvController animated:YES]

这是我在detailviewcontroller上的自我视图功能:

-(void)loadView {

    [super loadView];

    self.title=@"Month"

    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"ListView" style:UIBarButtonItemStyleDone target:self action:@selector(add:)];
    self.navigationItem.rightBarButtonItem = addButtonItem;

    calendarView = [[[KLCalendarView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,  320.0f, 373.0f) delegate:self] autorelease];
    appDelegate1 = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];

    myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 260, 320, 160)style:UITableViewStylePlain];
    myTableView.dataSource=self;
    myTableView.delegate=self;
    UIView *myHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, myTableView.frame.size.width,2)];
    myHeaderView.backgroundColor=[UIColor grayColor];
    [myTableView setTableHeaderView:myHeaderView];

    [self.view addSubview:myTableView];
    [self.view addSubview:calendarView];
    [self.view bringSubviewToFront:myTableView];
}

我认为在这里自我加载我需要制作if程序.. 如果indexPath.row = x解析fisier.php?variabila = title_of_rowx 但问题是我如何知道indexPath变量?

1 个答案:

答案 0 :(得分:0)

您可以在BookDetailViewController上设置属性:

BookDetailViewController.h:

@interface BookDetailViewController : UITableViewController {
    NSIndexPath *selectedIndexPath;
}
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
@end

BookDetailViewController.m

@implementation BookDetailViewController
@synthesize selectedIndexPath;
- (void)dealloc {
    [selectedIndexPath release];
}
// ...
@end

的tableView:didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

    if(bdvController == nil) {
        bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]];
    }
    bdvController.selectedIndexPath = indexPath
    Villa *aVilla = [appDelegate.villas objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:bdvController animated:YES]
}

根据您在应用程序中执行的操作,将属性设置为Villa对象而非NSIndexPath可能更有意义。