我正在尝试制作一个涉及 tableView 的简单程序。当我单击 tableView 中的单元格时,它应该显示有关单击的单元格的更多信息DetailedViewController
。但是,当我单击单元格时,它仍然突出显示,并且不会显示单元格的信息。所以我使用NSLog()
语句来确定程序卡住的地方。NSLog()
我在方法中引入的语句didDeselectRowAtIndexPath
表明我得到了在最后一个之前单击的单元格的响应。
知道为什么会这样吗?另外,为什么单击单元格时看不到DetailedViewController?
为简洁起见,Numbers.h
包含两个变量:NSString * name
和int long long number
。DetailedViewController 是一个简单的 VC,有两个标签:当我单击相应的单元格时,它们应该会得到更新nameField
。numberField
我真诚地感谢任何帮助。谢谢。
#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