ios - didSelectRowAtIndexPath 只被调用一次

标签 ios uiviewcontroller uitableview didselectrowatindexpath

我有两个 View Controller ,FirstViewController 和 DetailViewController,来回切换。

在 didSelectRowAtIndexPath 方法中 ---> updateRowNumber:indexPath.row,我将选定的行号传递给 DetailViewController。

第一次成功,FirstViewController可以将选中的行传递给DetailedViewController,来回切换后,它仍然得到最后选中的行。

有人知道我错过了什么吗?

#import "FirstViewController.h"
#import "AppDelegate.h"
#import "DetailViewController.h"
#import "PartyListCustomViewCell.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize dvController;  // it is a DetailViewController

- (void) viewDidLoad
{
contentArray = [[NSArray arrayWithObjects:@"heyun", @"wushi", @"ios", nil]retain];

[super viewDidLoad];

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    [dvController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
}

- (void)viewDisDisappear:(BOOL)animated
{
    [super viewDidDisappear: animated];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [contentArray count];
}

// custom row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // dynamic with whether the cell has the party image or not..
    return 160;
}

// Customize the appearance of table view cells
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:        (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PartyListCustomCell";

    PartyListCustomViewCell *plcc = [tableView     dequeueReusableCellWithIdentifier:CellIdentifier];

    if(plcc == nil)
    {
        plcc = [[[PartyListCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    // set up custom cell
    plcc.partyTitle.text = @"partyTitle";


    return plcc;

}

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

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

        self.dvController = aController;

        [aController release];
    }   

    [dvController updateRowNumber:indexPath.row];
    [[self navigationController] pushViewController:dvController animated:YES];

}

@end



#import "DetailViewController.h"


@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize rowNumber;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

- (void) updateRowNumber: (int) theindex
{

    rowNumber = theindex + 1;    
}

- (void)dealloc
{

    [super dealloc];
}

- (void)viewDidLoad
{

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];

    label1.text = [NSString stringWithFormat:@"row %i was clicked ", rowNumber];

    [self.view addSubview: label1];

    [super viewDidLoad];
    [label1 release];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{   
    [super viewWillAppear: animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最佳答案

首先,如果“dvController”是一个属性,请将“didSelectRowAtIndexPath”方法中的“dvController”引用更改为“self.dvController".

其次,在您的“DetailViewController”对象中,当“updateRowNumber”方法触发时...看起来您没有做任何其他事情(即更新 View 或其他)之后,除了更新内部行号。

关于ios - didSelectRowAtIndexPath 只被调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329609/

相关文章:

ios - 如何在可变高度的 UITableView 中根据内容确定 UIWebView 的高度?

ios - 在 UITableView 中滚动时图像变得困惑

ios - 如何快速双击(而不是单击) Collection View 单元格?

ios - 解除没有固定顺序的 View Controller

ios - 仅关闭 ImagePicker 并将图像传递给 ScrollViewController

swift - 使新的 TableView 单元格 textField 成为第一个响应者

ios - SceneKit 中的 "simd"前缀是什么意思?

ios - 如何在UIView中为UIImageView添加tapGesture

ios - UIStoryboard 和继承

ios - UITableView - 主标题和节标题之间的间隙