DetailedViewController

DetailedViewController

我正在尝试制作一个涉及tableView的简单程序。当我在tableView中单击一个单元格时,它应该显示有关在DetailedViewController上单击的单元格的更多信息。但是,当我单击该单元格时,它将保持突出显示状态,并且不会显示该单元格的信息。因此,我使用了NSLog()语句来确定程序在哪里卡住了。我在NSLog()方法中引入的didDeselectRowAtIndexPath语句表明,对于在上一个单元格之前单击的单元格,我得到了响应。

知道为什么会这样吗?另外,为什么单击单元格时看不到DetailedViewController?

为了简洁起见,Numbers.h包含两个变量:NSString * nameint long long number。 DetailedViewController是一个简单的VC,带有两个标签:nameFieldnumberField,当我单击相应的单元格时,它们应该被更新。

衷心的感谢。谢谢。

#import "SecondViewController.h"
#import "DetailedViewController.h"
#import "Numbers.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize numberList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"loaded Nib file");
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"View Numbers";
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"Loaded view controller");
    Numbers *num1 = [[Numbers alloc] init];
    Numbers *num2 = [[Numbers alloc] init];
    num1.name = @"Number 1";
    num1.number = 12345;

    num2.name = @"Number 2";
    num2.number = 23456;

    numberList = [[NSMutableArray alloc]  initWithObjects:num1,num2, nil];

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload {
    [self setNumberList:nil];
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberList.count;
}

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

- (UITableViewCell *)tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    NSLog(@"In cellForRowAtIndexPath:");
    static NSString *cellIndetifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2    reuseIdentifier:cellIndetifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }

    cell.textLabel.text = [[numberList objectAtIndex:indexPath.row] name];

    return cell;

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row selected = %d", indexPath.row);

    Numbers *numberToDisplay = [[Numbers alloc] init];
    numberToDisplay.name = [[numberList objectAtIndex:indexPath.row] name]; // why can I not use dot notation here?
    numberToDisplay.number = [[numberList objectAtIndex:indexPath.row] number]; // why can I not use dot notation here?

    DetailedViewController *detailedViewController = [[DetailedViewController alloc] initWithNibName:@"DetailedViewController" bundle:nil];
    detailedViewController.nameLabel.text = numberToDisplay.name;
    detailedViewController.numberLabel.text = [NSString stringWithFormat:@"%lli", numberToDisplay.number];

    [self.navigationController pushViewController:detailedViewController animated:YES];
}

@end

最佳答案

didDeselectRowAtIndexPath更改为didSelectRowAtIndexPath。这应该为您提供有关当前所选单元格的信息。

从didDeselectRowAtIndexPath获取的单元格是在选择新单元格后被取消选择的行。这就是为什么它落后了。

10-08 06:04