从UItableViewCell调用视图

时间:2013-08-19 01:30:29

标签: iphone ios xcode uitableview

我正在尝试制作我的第一款应用。我有6个文件:

LeftPanelViewController.h LeftPanelViewController.m LeftPanelViewController.xib ForecastViewController.h ForecastViewController.m ForecastViewController.xib

在LeftPanelViewController.xib中,我有一个包含表视图的视图。我想从我在LeftPanelViewController.xib中的表视图中设置一个单元格,所以当我按下那个单元格时,我将能够调用我在ForecastViewController.xib中的视图。

在我的LeftPanelTableViewController.m

代码下面
#import "LeftPanelViewController.h"
#import "ForecastViewController.h"


@interface LeftPanelViewController ()

@property (nonatomic, weak) IBOutlet UITableView *myTableView;
@property (nonatomic, weak) IBOutlet UITableViewCell *cellMain;
@property (copy, nonatomic) NSArray *MRP;

@end

@implementation LeftPanelViewController

- (void)viewDidLoad
{
[super viewDidLoad];

Array = [[ NSMutableArray alloc] init];
[Array addObject:@"Forecast"];
[Array addObject:@"Inventory"];

}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier];

//add an image to the tablecell
UIImage *image = [UIImage imageNamed:@"Icon.png"];
cell.imageView.image = image;

}
cell.textLabel.text = [Array objectAtIndex:indexPath.row];

if (indexPath.row < 7) {
cell.detailTextLabel.text = @"Account";
} else {
cell.detailTextLabel.text = @"Settings";
}
return cell;
}

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

#pragma mark -
#pragma mark View Will/Did Appear

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

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

#pragma mark -
#pragma mark View Will/Did Disappear

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

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

#pragma mark -
#pragma mark Array Setup


#pragma mark -
#pragma mark UITableView Datasource/Delegate

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


#pragma mark -
#pragma mark Default System Code

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

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

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

if ([[Array objectAtIndex:indexPath.row] isEqual:@"Forecast"]) {

ForecastViewController *Forecast = [[ ForecastViewController alloc] initWithNibName:@"ForecastViewController" bundle:nil];
[self.navigationController pushViewController:Forecast animated:YES];

}


[tableView deselectRowAtIndexPath:indexPath animated:YES];
}



@end

2 个答案:

答案 0 :(得分:1)

而不是

[self.navigationController pushViewController:Forecast animated:YES];

使用

[self presentViewController:Forecast animated:YES completion:nil];

答案 1 :(得分:1)

即使模态控制器完成了这个技巧,也可能不是你想要的。什么都没发生,因为你的视图控制器是零。可能你正在使用xCode的“单一视图应用程序”模板,它没有导航控制器。如果你想要一个,你应该创建它[[UINavigationController alloc] initWithRootViewController:]并将你的窗口根控制器设置为新创建的navigationcontroller。